0
Intent i = new Intent(getActivity(), MytabActivity.class);
startActivity(i);

My DialogFragment opens when a button is clicked from the calling activity. I does what it's asked for and was able to function all the part of the code except opening to another activity after a successful response from the server (I'm using volley). The values did change but it crashes in this part.

Any ideas?

Here a screenshot of the code.

enter image description here

logcat:

03-05 03:30:11.570  16952-16952/com.example.carlajoyce.ccare E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.carlajoyce.ccare, PID: 16952
java.lang.NullPointerException
        at android.widget.Toast.<init>(Toast.java:93)
        at android.widget.Toast.makeText(Toast.java:241)
        at com.example.carlajoyce.ccare.UpdateDialogFragment$2$1.onResponse(UpdateDialogFragment.java:73)
        at com.example.carlajoyce.ccare.UpdateDialogFragment$2$1.onResponse(UpdateDialogFragment.java:64)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
        at dalvik.system.NativeStart.main(Native Method)

1 Answers1

0

The crash may be because you call the Intent from background thread. Make sure you are calling the Intent from Main Thread inside your Activity

Try to cast your intent inside the following:

context.runOnUiThread(new Runnable() {
    public void run() {
        //INTENT HERE
    }
});
galhe2
  • 165
  • 1
  • 4
  • 10
  • The IDE cannot resolve runOnUiThread because my class extends to `DialogFragment` not `Activity` – aspiringOne Mar 04 '16 at 19:45
  • Are you able to get the context inside the DialogFragment? If yes, add it before the runOnUiThread (see edited answer) You can get the context by calling the following: getApplicationContext() – galhe2 Mar 04 '16 at 19:51
  • I'm able to get the thread by `getActivity().runOnUiThread(new Run.....` but the error is still there in the same spot. – aspiringOne Mar 04 '16 at 20:02