2013-04-22 223 views
1

我正在Android中創建一個應用程序,但是當我想更新一些數據時,我想重新啓動我的MainActivity。但是,當我嘗試重新啓動應用程序時出現問題,它崩潰了。如何重新啓動Android活動?

我用下面的代碼重新啓動我的活動

case R.id.update: 
     admin = false; 
     Intent intent1 = getIntent(); 
     finish(); 
     startActivity(intent1);  
     break; 

我的日誌是

04-22 12:28:10.444: E/AndroidRuntime(11867): FATAL EXCEPTION: main 
04-22 12:28:10.444: E/AndroidRuntime(11867): java.lang.RuntimeException: Unable to start activity ComponentInfo{package_____________/package________.LauncherGridActivity}: java.lang.NullPointerException 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.os.Looper.loop(Looper.java:137) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at java.lang.reflect.Method.invokeNative(Native Method) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at java.lang.reflect.Method.invoke(Method.java:511) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at dalvik.system.NativeStart.main(Native Method) 
04-22 12:28:10.444: E/AndroidRuntime(11867): Caused by: java.lang.NullPointerException 
04-22 12:28:10.444: E/AndroidRuntime(11867): at com.___________.ApplicationManager.loadFilteredNames(ApplicationManager.java:228) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at com______.ApplicationManager.loadApplications(ApplicationManager.java:65) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at co___.LauncherGridActivity.onCreate(LauncherGridActivity.java:142) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.Activity.performCreate(Activity.java:4465) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
04-22 12:28:10.444: E/AndroidRuntime(11867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
04-22 12:28:10.444: E/AndroidRuntime(11867): ... 11 m 
+0

意向intent1 =新意圖(MainActivity.this,MainActivity.class); startActivity(intent1);完(); – Raghunandan 2013-04-22 10:40:21

+0

重新啓動時,代碼中的loadFilteredNames()方法有問題。發佈該代碼。 – 2013-04-22 10:41:52

+1

at com.___________。ApplicationManager.loadFilteredNames(ApplicationManager.java:228) 此行的某些變量爲null。請確保在使用之前初始化它。 我用上面相同的代碼重新啓動,它工作正常。所以也許問題是一個null變量。請發佈上述指定的行 – 2013-04-22 10:43:48

回答

1

使用此您可以重新啓動活動。

Intent intent = getIntent(); 
finish(); 
startActivity(intent); 

但是按照您的要求,你需要使用重新創建活動this.recreate();

+0

用戶已經嘗試使用相同的代碼。 – 2013-04-22 10:44:58

0

嘗試這樣

Intent intent = new Intent(YourActivityName.this,YourActivityName.class); 

startActivity(intent); 

finish();