gpt4 book ai didi

android - 相同的代码在 Release模式下不起作用(android共享菜单)

转载 作者:行者123 更新时间:2023-12-02 13:43:31 24 4
gpt4 key购买 nike

以下代码在 Debug模式下完美运行:

menu_post_view.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.mydream.bgz.activities.PostViewActivity">
<item
android:id="@+id/action_share"
android:title="@string/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"/>
</menu>

PostViewActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_post_view, menu);

ShareActionProvider mShareActionProvider;
MenuItem item = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// mShareActionProvider.setOnShareTargetSelectedListener(this);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, str_mPostTitle);
sharingIntent.putExtra(Intent.EXTRA_TEXT, str_mPostUrl);
mShareActionProvider.setShareIntent(sharingIntent);

return super.onCreateOptionsMenu(menu);
}

一切都发生在onCreate()方法中。我用 try-catch 包围了所有代码,并得到了以下打印的堆栈跟踪。

06-13 20:36:41.817 689-689/? W/SupportMenuInflater: Cannot instantiate class: android.support.v7.widget.ShareActionProvider
java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.ShareActionProvider" on path: DexPathList[[zip file "/data/app/com.mydream.bgzaz-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mydream.bgzaz-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)
at android.support.v7.view.g$b.a(Unknown Source)
at android.support.v7.view.g$b.b(Unknown Source)
at android.support.v7.view.g.a(Unknown Source)
at android.support.v7.view.g.inflate(Unknown Source)
at com.mydream.bgz.activities.PostViewActivity.onCreateOptionsMenu(Unknown Source)
at android.app.Activity.onCreatePanelMenu(Activity.java:2881)
at android.support.v4.b.l.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.f$a.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.o.j(Unknown Source)
at android.support.v7.app.o$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Suppressed: java.lang.ClassNotFoundException: android.support.v7.widget.ShareActionProvider
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)
... 20 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
06-13 20:36:41.822 689-689/? D/AndroidRuntime: Shutting down VM


--------- beginning of crash
06-13 20:36:41.823 689-689/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mydream.bgzaz, PID: 689
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.bf.a(android.content.Intent)' on a null object reference
at com.mydream.bgz.activities.PostViewActivity.onCreateOptionsMenu(Unknown Source)
at android.app.Activity.onCreatePanelMenu(Activity.java:2881)
at android.support.v4.b.l.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.f$a.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.o.j(Unknown Source)
at android.support.v7.app.o$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
06-13 20:36:41.828 1313-3539/? W/ActivityManager: Force finishing activity com.mydream.bgzaz/com.mydream.bgz.activities.PostViewActivity

build.gradle

compile files('libs/YouTubeAndroidPlayerApi.jar')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-vector-drawable:25.3.1'

我从来没有遇到过这样的问题。所以,我无法理解为什么一些正常代码不能在 Release模式下工作,而是在调试构建版本下工作。请注意,我没有在 onCreate() 中编写任何与共享操作菜单相关的代码。请再次帮助我理解为什么会出现此问题以及如何解决它?

最佳答案

那是因为您在 Release模式下混淆了代码。

将其添加到您的 proguard 配置文件中。

-keep class android.support.v7.internal.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.internal.** { *; }
-keep interface android.support.v7.** { *; }

关于android - 相同的代码在 Release模式下不起作用(android共享菜单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44527305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com