How to restart an Android Activity?

谁说胖子不能爱 提交于 2019-12-24 03:37:25

问题


I am creating an application in Android, but when I would like to update some data, I want to restart my MainActivity. But my problem when I try to restart my Application, it crashes.

I use the following code to restart my Activity

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

my Log is

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

回答1:


Use this you can restart activity.

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

But as per your requirement you need to recreate activity using this.recreate();




回答2:


try it like this

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

startActivity(intent);

finish();


来源:https://stackoverflow.com/questions/16145056/how-to-restart-an-android-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!