Reputation: 3621
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();
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.
Upvotes: 1
Views: 15785
Reputation: 597
Create a static Activity object which activity finish on other activity and assign activity in this i.e you can can add more activities
public class demoActivity extends AppCompatActivity {
public static Activity self_intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_activity);
selfintent=this;
}
//Other functions--------------
}
do same for other activities
on other
activityCloseBtn= (Button) view.findViewById(R.id.activity_close_btn);
activityCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
demoActivity.selfintent.finish(); //for finish demoActivityactivity
//for other activities Activity.selfintent.finish();
finish(); //for finish current activity
}
});
Upvotes: -1
Reputation: 4620
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)
Upvotes: 2
Reputation: 2140
i think there are little conceptual problem Actually FLAG_ACTIVITY_CLEAR_TOP flag is Use is check here: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP so in that case there are no need finish() method it is already Remove mainActivity class but according to document you have to add one more Flag FLAG_ACTIVITY_NEW_TASK to your intent.
So Replace
Intent i = new Intent(MainActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
with
Intent i = new Intent(MainActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
thats it...
Upvotes: 8