今天,我正在使用最新的Android 7.1功能应用程序快捷方式制作应用程序。起初,它在Android 7.1.1上运行良好。但是,当我在Android M上的MIUI 8上运行时,不幸的是它崩溃了。我写了一些东西让它在M或更低版本上运行,但它再次破裂。我很困惑。我希望有人可以帮我解决这个问题。如果有人可以帮助我,我将不胜感激。不管怎么说,还是要谢谢你!
这是我的代码。
findViewById(R.id.textView5).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addshortcut();
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
shortcutManager = getSystemService(ShortcutManager.class);
}
public void addshortcut(){
TextView textView = (TextView)findViewById(R.id.textView5);
String name = textView.getText().toString();
ShortcutInfo dynamicShortcut = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
dynamicShortcut = new ShortcutInfo.Builder(this, "shortcut_dynamic-zty")
.setShortLabel(name)
.setLongLabel(name)
.setIcon(Icon.createWithResource(this,R.drawable.ic_account_circle))
.setIntents(
new Intent[]{
new Intent(Intent.ACTION_VIEW, Uri.EMPTY,com.zengtianyu.gallery1.DetailZTY.this, DetailZTY.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
})
.build();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
shortcutManager.setDynamicShortcuts(Arrays.asList(dynamicShortcut));
}
Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
}
这是日志。
java.lang.NoClassDefFoundError: Failed resolution of:Landroid/content/pm/ShortcutManager;
at com.zengtianyu.gallery1.DetailLSW.onCreate(DetailLSW.java:88)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2518)
at android.app.ActivityThread.access$1000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5544)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.content.pm.ShortcutManager" on path: DexPathList[[zip file "/data/app/com.zengtianyu.gallery1-1/base.apk"],nativeLibraryDirectories=[/data/app/com.zengtianyu.gallery1-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 14 more
Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.content.pm.ShortcutManager" on path: DexPathList[[dex file "/data/dalvik-cache/xposed_XResourcesSuperClass.dex"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 15 more
Suppressed: java.lang.ClassNotFoundException:android.content.pm.ShortcutManager
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
答案 0 :(得分:2)
您正在传递ShortcutManager.class
而不是字符串常量SHORTCUT_SERVICE
将第8行更改为以下内容:
shortcutManager = (ShortcutManager)getSystemService(SHORTCUT_SERVICE);
如果您希望支持较旧的Android版本,请不要将shortcutManager设为全局变量,而是在addShortcut()函数中创建它,并将该函数的用法包含在第7行的sdk版本检查中
所以你最终应该:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
addShortcut();
}
...
public void addShortcut(){
ShortcutManager shortcutManager = (ShortcutManager)getSystemService(SHORTCUT_SERVICE);
...
}