1

I'm new and learning android studio and understanding it more, but I'm stuck here. I can't reload my mainActivity using a button in a different class which has a custom Dialog. I've searched many solutions but none of them work, it always end up 'NullPointerException' error. Can anyone please help me understand this problem more.

this is my mainAcitivity

`

public class MainActivity extends AppCompatActivity {

DialogHandler dialogHandler;
Context mainContext;
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainContext = this;
    dialog = new Dialog(this);
    dialogHandler = new DialogHandler(MainActivity.this);

    Button showButton = (Button) findViewById(R.id.showButton);

    showButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialogHandler.showDialog(mainContext);
        }
    });
}

`

and this is my second class (DialogHandler)

'

public class DialogHandler
{
   Activity activity;
   MainActivity mainActivity;
   Dialog dialog;

public DialogHandler (Activity activity)
{
    this.activity = activity;
}

public void showDialog (Context context)
{
    mainActivity = new MainActivity();
    dialog = new Dialog(context);


    dialog.setContentView(R.layout.activity_dialog);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();

    Button changeButton = dialog.findViewById(R.id.changeButton);

    changeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mainActivity.finish();
            mainActivity.startActivity(mainActivity.getIntent()); // error occurs this line
        }
    });
}

} '

This is my error

'

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dialogpractice, PID: 30511
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
    at android.app.Activity.startActivityForResult(Activity.java:3843)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
    at android.app.Activity.startActivityForResult(Activity.java:3804)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
    at android.app.Activity.startActivity(Activity.java:4114)
    at android.app.Activity.startActivity(Activity.java:4082)
    at com.example.dialogpractice.DialogHandler$1.onClick(DialogHandler.java:41)
    at android.view.View.performClick(View.java:4781)
    at android.view.View$PerformClick.run(View.java:19907)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:160)
    at android.app.ActivityThread.main(ActivityThread.java:5541)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

'

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • can you please add stacktrace of the exception also – keshav kowshik Apr 14 '20 at 17:48
  • Substitute this two lines: mainActivity.finish(); mainActivity.startActivity(mainActivity.getIntent()); – MMG Apr 14 '20 at 17:54
  • @Keshav1234 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dialogpractice, PID: 30511 java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference – Schweizer_Ken Apr 14 '20 at 21:41
  • @Keshav1234 the error occurs when I try to start the activity in ' mainActivity.startActivity(mainActivity.getIntent()); ' – Schweizer_Ken Apr 14 '20 at 21:43

1 Answers1

0

You are passing activity reference to DialogHandler even then why are you creating object of MainActivity.

use activity.finish instead of mainActivity.finish

If activity.finish does not work you can try this:

if (context is MainActivity){
  ((MainActivity) context).finish;
}

Instead of this line:

mainActivity.startActivity(mainActivity.getIntent());

Can you try doing this

startActivity(new Intent(context, MainActvity.class))

Hope this helps

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • sorry, I still got an error E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dialogpractice, PID: 30511 java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference – Schweizer_Ken Apr 14 '20 at 21:45
  • @Schweizer_Ken check my updated answer. try that this should work. – keshav kowshik Apr 15 '20 at 04:27
  • it worked! what I did was using the 'activity.finish()' thank you so much! – Schweizer_Ken Apr 15 '20 at 21:19
  • @Schweizer_Ken please accept it as answer and upvote it if my answer helped you. – keshav kowshik Apr 16 '20 at 04:59