请帮忙,我已经深入研究了 onSaveInstanceState 和 onConfigChange 的东西,但无法获得图片。
我通过 Google Play 发布的应用程序报告了异常,但我无法在我的开发环境中重现它。经过跟踪,我发现PreviewActivity的onCreate方法中发生了2个NullPointerException:
问题一:
(一个)。PreviewActivity 的 onCreate 方法中的代码:
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.logo_preview_layout);
...
mBtnCheckGuess = (Button)findViewById(R.id.btn_check_guess);
mVgGuessPanel = (View)findViewById(R.id.layout_guess); // the exception line!
...
}
(b)。布局 xml 中的代码(只是其中的一部分)
<ScrollView
android:gravity="center_horizontal"
android:orientation="vertical"
android:id="@+id/main_layout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginTop="25.0dip" android:layout_marginBottom="10.0dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/layout_guess"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="0.0dip" android:layout_marginBottom="0.0dip"
android:visibility="visible">
我完全确定在这个布局 xml 文件中没有其他 @id/layout_guess 。
(C)。AndroidManifest.xml 中的代码
<activity
android:name=".PreviewActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="portrait">
</activity>
对于这个问题,我有几十个 NullPointerException,但是在我在 AndroidManifest 中删除了这一行之后:
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
这两天我没有遇到新版本应用程序的相同问题。我完全不知道为什么...
问题2: 背景,我想在A启动B时将一个对象从Activity A传递给Activity B,我使用Android的Application对象将该对象包含在其实例变量中。
(a) MyApplication 的代码:
public class MyApplication extends Application{
Logo mSelectedLogo;
@Override
public void onCreate(){
}
public void setSelectedLogo(Logo logo){
mSelectedLogo = logo;
}
public Logo getSelectedLogo(){
return this.mSelectedLogo;
}
}
(b) 活动 A 部分代码
mApplication.setSelectedLogo(logo);
Intent intent = new Intent(this, ActivityB.class);
LogoListActivity.this.startActivityForResult(intent, REQUEST_GUESS);
(c) 活动 B 代码
public void onCreate(Bundle savedInstanceState) {
...
MyApplication mApplication = (MyApplication)this.getApplication();
mSelectedLogo = mApplication.getSelectedLogo();
ALog.d(TAG, "correct logo name: " + mSelectedLogo.mName + ", id: " + mSelectedLogo.mId); // NullPointerException line, obviously mApplication.getSelectedLogo() returns NULL
...
}
为什么它会是NULL真的很奇怪。除非 MyApplication 对象已被销毁。
错误日志在这里:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codefes.quiz/com.codefes.quiz.PreviewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.codefes.quiz.PreviewActivity.onCreate(Unknown Source)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
请帮忙,谢谢!