NPE NPE - 1 month ago 17
Android Question

Android : destroy activity before intent to another activity

I have two activity in my android application :

1. LoginActivity : User can Login as member by using password and email or as guest

2. MainActivity : Showing user current location on map


Scenario :

1. User login as guest

2. Go to Main Activity as guest. (No NPE here)

3. User took some action that needed to login

4. Intent to LoginActivity

5. I need to destroy MainActivity before directing user to LoginActivity

6. User input username and password on LoginActivity

7. If user exist and password true go to MainActivity

8. NPE (or Null Pointer Exception) happened here.


So, in my case, i needed to destroy MainActivity (Point number 5) before directing user to LoginActivity. I've tried this :

final AlertDialog.Builder builder = new AlertDialog.Builder(context);

final String message = getResources().getString(R.string.NCI);

builder.setMessage(Html.fromHtml("To do your action, we need you to logged in our server"))
.setTitle("LOGIN REQUIRED")
.setPositiveButton("OK",
new OnClickListener(){
public void onClick(DialogInterface d, int id){
Intent i = new Intent(MainActivity.this, LoginActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
}
);

builder.create().show();


Log Cat



07-25 16:04:27.287: E/AndroidRuntime(19823): FATAL EXCEPTION: main
07-25 16:04:27.287: E/AndroidRuntime(19823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yai.properti.tujuh.tujuh.tujuh/com.yai.properti.tujuh.tujuh.tujuh.MainActivity}: java.lang.NullPointerException
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.os.Looper.loop(Looper.java:137)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-25 16:04:27.287: E/AndroidRuntime(19823): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:04:27.287: E/AndroidRuntime(19823): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 16:04:27.287: E/AndroidRuntime(19823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-25 16:04:27.287: E/AndroidRuntime(19823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-25 16:04:27.287: E/AndroidRuntime(19823): at dalvik.system.NativeStart.main(Native Method)
07-25 16:04:27.287: E/AndroidRuntime(19823): Caused by: java.lang.NullPointerException
07-25 16:04:27.287: E/AndroidRuntime(19823): at com.yai.properti.tujuh.tujuh.tujuh.MainActivity.onCreate(MainActivity.java:817)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.Activity.performCreate(Activity.java:5163)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-25 16:04:27.287: E/AndroidRuntime(19823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-25 16:04:27.287: E/AndroidRuntime(19823): ... 11 more


I've tried onDestroy() by replacing finish() call method, but NPE (Null Pointer Exception) still happened. As the first time intent to MainActivity, NPE not happended.

What i want
is start MainActivity as the first time
. How i could do that?

Many thanks.

Answer

Whenever you need to call finish() to finish some activity inside the anonymous inner class like you did in above code,just pass the complete activity's name(the one which you want to destroy) ,like MainActivity.this.finish()..(where MainActivity is the one which you want to destroy)