英文:
App crashes when downloaded from playstore
问题
代码部分不要翻译,只返回翻译好的内容:
主活动文件(pp main activity file)能够运行,但是当点击任何片段(fragment)时会崩溃。然而,在设备上以及 Android 模拟器上进行仿真时,它能够平稳运行。崩溃的日志(logcat)如下所示:
java.lang.NullPointerException: 尝试在空对象引用上调用接口方法 'java.lang.Object[] java.util.Collection.toArray()'
at java.util.ArrayList.addAll(ArrayList.java:588)
at d.a$b.a(:14)
at i.l$b$a$a.run(Unknown Source:37)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
与此同时,启动片段的主要活动(main activity)如下所示:
private void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
// 一系列 case 语句
default:
throw new IllegalStateException("Unexpected value: " + menuItem.getItemId());
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
assert fragment != null;
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
menuItem.setChecked(false);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
有人能指导一下为什么在 APK 文件中会出现错误,而在 Android Studio 上能够平稳运行吗?
英文:
The pp main activity file runs but when any fragment is clicked it crashes. however when emulated on device as well as android emulator, it runs smoothly. The logcat of crash is as below;
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at java.util.ArrayList.addAll(ArrayList.java:588)
at d.a$b.a(:14)
at i.l$b$a$a.run(Unknown Source:37)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
while the main activity which starts the fragment is as below;
private void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.menu_1:
fragmentClass = Fragment1.class;
break;
case R.id.menu_2:
fragmentClass = Fragment2.class;
break;
case R.id.menu_3:
fragmentClass = Fragment3.class;
break;
case R.id.menu_4:
fragmentClass = Fragment4.class;
break;
case R.id.menu_5:
fragmentClass = Fragment5.class;
break;
case R.id.menu_6:
fragmentClass = Fragment6.class;
break;
case R.id.menu_7:
fragmentClass = Fragment7.class;
break;
case R.id.menu_8:
fragmentClass = Fragment8.class;
break;
case R.id.menu_9:
fragmentClass = Fragment9.class;
break;
case R.id.menu_10:
fragmentClass = Fragment10.class;
break;
case R.id.menu_11:
fragmentClass = Fragment11.class;
break;
default:
throw new IllegalStateException("Unexpected value: " + menuItem.getItemId());
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
assert fragment != null;
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(false);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
can somebody guide why this is giving error in apk file while runs smoothly on android studio
答案1
得分: 0
在我的情况下,这些片段没有启动,所以我尝试从gradle中删除minifyEnabled和ShrinkResources(true),并将其更改为:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
尽管这增加了应用程序的大小,但崩溃问题得到了解决。
英文:
In my case, the fragments were not starting so I tried removing the minifyEnable and ShrinkResources (true) from the gradle and changed it as
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Although it increased the size of app but the crashing is resolved.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论