first try
首先在github上搜索slider preference ,看到这个
https://github.com/jayschwa/AndroidSliderPreference
用的是readme中,gradle里的方法,很方便
报错如下,参考了Stack Overflow,发现我用的是preference fragment compt ,
import android.support.v7.preference.PreferenceFragmentCompat;,
而看了他的源码,他的
SliderPreference extends DialogPreference
import android.preference.DialogPreference;
看来这个类库用的不是这个
cannot be cast to android.support.v7.preference.Preference
second try
发现安卓没有slider 这个控件,有的是seekbar
于是在github搜索,seekbar preference
找到一个:
https://github.com/DreaminginCodeZH/SeekBarPreference
里面专门声明了:
This Preference is based on support library preference-v7.
或许能用
看看源码
import android.support.v7.preference.PreferenceDialogFragmentCompat;
public class SeekBarPreferenceDialogFragment extends PreferenceDialogFragmentCompat {
但是它没说咋用,就先clone下来瞄一下
作为modeul import了一下,报错:
Could not get unknown property 'ANDROID_COMPILE_SDK_VERSION' for project ':library' of type org.gradle.api.Project.
根据资料:https://blog.csdn.net/guozhaohui628/article/details/72770372
这个在library所在的根gradle中定义了,却没在他这个modeul中定义
library的gradle:
/*
* Copyright (c) 2015 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
* All Rights Reserved.
*/
apply plugin: 'com.android.library'
android {
compileSdkVersion Integer.parseInt(project.ANDROID_COMPILE_SDK_VERSION)/////
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion 7
targetSdkVersion Integer.parseInt(project.ANDROID_TARGET_SDK_VERSION)/////
versionCode Integer.parseInt(project.VERSION_CODE)
versionName project.VERSION_NAME
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:preference-v7:23.4.0'
}
apply from: '../gradle-mvn-push.gradle'
因此尝试将这两个值改为自己项目对应的值,打开自己项目app modeul的gradle文件,里面写的compile 和 target sdk version 都是28,
然后自己项目的library的gradle文件,改为28
/*
* Copyright (c) 2015 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
* All Rights Reserved.
*/
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:preference-v7:23.4.0'
}
apply from: '../gradle-mvn-push.gradle'
再次sync ,报错:
Could not read script 'E:\project\AndroidProjects\SmartDiaper\gradle-mvn-push.gradle' as it does not exist.
看到现在library 的gradle 里面最后一句话,emm,要么注释掉,要么把demo里面的复制过来,先注释看看
Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
再把所有compile改过来,然后把dependencies 里面版本全改成28.0.0
终于sync成功了
在preference_setting.xml中加上
<me.zhanghai.android.seekbarpreference.SeekBarPreference
android:key="ring_volume"
android:max="100"
android:defaultValue="50"
android:title="音量"
android:summary="请选择音量" />
运行
报错:
Caused by: java.lang.ClassNotFoundException: Didn't find class "me.zhanghai.android.seekbarpreference.SeekBarPreference" on path: DexPathList[[zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/base.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_resources_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.smartdiaper-lZ-XyddGFZFsT7Gtj37Nog==/lib/arm64, /system/lib64, /vendor/lib64]]
当时直接就准备用源码了
现在看到这个解决办法,就是关掉instant run,准备日后开发结束后考虑用上
https://blog.csdn.net/mhy1025/article/details/83345390
直接用源码吧。。也不依赖啥库了。。。
添加了四个文件,如图红色的:
preference_setting.xml中:
<com.example.admin.smartdiaper.seekbarpreference.SeekBarPreference
android:key="ring_volume"
android:max="100"
android:defaultValue="50"
android:title="音量"
android:summary="请选择音量" />
可以打开了,但单击报错:
java.lang.IllegalArgumentException: Tried to display dialog for unknown preference type. Did you forget to override onDisplayPreferenceDialog()?
at android.support.v7.preference.PreferenceFragmentCompat.onDisplayPreferenceDialog(PreferenceFragmentCompat.java:654)
at android.support.v7.preference.PreferenceManager.showDialog(PreferenceManager.java:552)
at android.support.v7.preference.DialogPreference.onClick(DialogPreference.java:263)
这时候就要看用法了,记得github的readme里面其实写了
在SetupFragment.java 中加上
@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (!SeekBarPreference.onDisplayPreferenceDialog(this, preference)) {
super.onDisplayPreferenceDialog(preference);
}
}
ok,但是summary显示的是“请选择音量”。
现在把summary按照他的demo改成:
<com.example.admin.smartdiaper.seekbarpreference.SeekBarPreference
android:key="ring_volume"
android:max="100"
android:defaultValue="50"
android:title="音量"
android:summary="[ %1$s%% ]" />
就可以实时显示了~
这是怎么做到的呢?在源码中找到了这个:
还是想用别人的库
还是didn't find class
重启Android studio
还是didn't find
我是不是没在app里添加对library的依赖!!
是的啊!!
problem solved