从活动组启动活动时出现Nullpointer异常

时间:2013-05-15 10:26:02

标签: android android-layout android-intent

我正在尝试从活动组启动活动。以下是我使用的代码:

public void replaceContentView() {
    Intent i = new Intent(SummaryCostScreen.this,PermissionsScreen.class);
    View view = getLocalActivityManager().startActivity("activity9",i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    this.setContentView(view);
    } 

我得到了nullpointer异常,如图所示:

05-15 09:49:23.367: D/AndroidRuntime(3872): Shutting down VM
05-15 09:49:23.367: W/dalvikvm(3872): threadid=1: thread exiting with uncaught exception (group=0x40b1a1f8)
05-15 09:49:23.382: E/AndroidRuntime(3872): FATAL EXCEPTION: main
05-15 09:49:23.382: E/AndroidRuntime(3872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nykkos.personalize/com.nykkos.personalize.launcher.screens.PermissionsScreen}: java.lang.NullPointerException
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1964)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1799)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at com.nykkos.personalize.launcher.screens.SummaryCostScreen.replaceContentView(SummaryCostScreen.java:255)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at com.nykkos.personalize.launcher.screens.SummaryCostScreen.onClick(SummaryCostScreen.java:116)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.view.View.performClick(View.java:3480)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.view.View$PerformClick.run(View.java:13993)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.os.Handler.handleCallback(Handler.java:605)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.os.Looper.loop(Looper.java:137)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.ActivityThread.main(ActivityThread.java:4368)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at java.lang.reflect.Method.invoke(Method.java:511)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at dalvik.system.NativeStart.main(Native Method)
05-15 09:49:23.382: E/AndroidRuntime(3872): Caused by: java.lang.NullPointerException
05-15 09:49:23.382: E/AndroidRuntime(3872):     at com.nykkos.personalize.launcher.screens.PermissionsScreen.onCreate(PermissionsScreen.java:48)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.Activity.performCreate(Activity.java:4465)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-15 09:49:23.382: E/AndroidRuntime(3872):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
05-15 09:49:23.382: E/AndroidRuntime(3872):     ... 16 more

抛出Nullpointer
getLocalActivityManager().startActivity("activity9",i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

这个nullpointer的原因可能是什么,虽然我看不出任何明显的原因。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

请使用更好方法的碎片

此类在API级别13中已弃用。 请改用新的Fragment和FragmentManager API;这些也可以通过Android兼容包在旧版平台上使用。

http://developer.android.com/reference/android/app/ActivityGroup.html

答案 1 :(得分:1)

我从logcat描述中看到的是,您的PermissionsScreenonCreate()方法中的第48行存在异常。

答案 2 :(得分:1)

Activity ac = (Activity) context;   
ActivityGroup ag = (ActivityGroup) ac.getParent();   
Intent i = new Intent(SummaryCostScreen.this,PermissionsScreen.class);
View view =  ag.getLocalActivityManager().startActivity("activity9",i.addFlags
(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);

试试这个。