2

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
midhunhk
  • 5,560
  • 7
  • 52
  • 83
diez
  • 125
  • 1
  • 5
  • 12
  • Intent intent1 = new Intent(MainActivity.this,MainActivity.class); startActivity(intent1); finish(); – Raghunandan Apr 22 '13 at 10:40
  • Something is wrong with `loadFilteredNames()` method in your code, when you restart. Post that code. – S.D. Apr 22 '13 at 10:41
  • 1
    at com.___________.ApplicationManager.loadFilteredNames(ApplicationManager.java:228) Some variable is null at this line .Make sure you initialize it before using . I used the above same code to restart and it worked fine. So perhaps the problem is with a null variable.Pls post the above specified line – Rachita Nanda Apr 22 '13 at 10:43
  • 5
    I think you have a design flaw with your system as a whole. You do not need to restart activity to reload data. That simply is not how it's done. – Budius Apr 22 '13 at 10:50
  • 1
    Correct, as Mr Budius is saying, to update your data only, don't finish and startActivity again and again, its heavy task for Android system, its this process Activity manager, Task Manager everything will be included, find another way to update data only, if you can tell the scenario we can help you in how to update data of activity instead of recreating it – umesh Apr 22 '13 at 11:00
  • There's something wrong here. I use the exact code that user1621629 posted and it works perfectly for me. Something's missing. – Edward Falk Jan 11 '14 at 21:59
  • But that said, Budius is right; you don't normally need to restart an activity like this. I only do it when I change my app's theme. – Edward Falk Jan 11 '14 at 21:59

2 Answers2

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();

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
user1621629
  • 765
  • 7
  • 19
1

try it like this

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

startActivity(intent);

finish();
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
MOK3K
  • 11
  • 2