简体   繁体   中英

Cannot start intent from different activity

I have this setup in my code.

XML:

    <Button
    android:id="@+id/planTrip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/clearDestination"
    android:layout_below="@+id/destination"
    android:onClick="buttonCalls"
    android:text="@string/plan_trip" />

MainActivity

    public void buttonCalls(View view) {
    Utils util = new Utils();
    switch (view.getId()) {
    case (R.id.planTrip):
        System.out.println("PLANTRIP BUTTON");
        try {
            util.planTripUtil(MainActivity.this);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    default:
        break;
    }

}

Utils Activity

    public void planTripUtil(Context context) throws IOException {
    boolean canGetStartCoord = GC.canGetCoordinates;
    boolean canGetEndCoord = GC.canGetCoordinates;

    if (canGetEndCoord && canGetStartCoord) {
        Intent intent = new Intent(context, WebViewActivity.class);
        startActivity(intent);

}

I'm getting this error whenever i click the button. I know the problem is with the startActivity(intent), it just gives my an IllegalStateException everytime. Any ideas why and how do i solve this?

    08-04 15:04:05.599: E/AndroidRuntime(23230): FATAL EXCEPTION: main
08-04 15:04:05.599: E/AndroidRuntime(23230): java.lang.IllegalStateException: Could not execute method of the activity
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.view.View$1.onClick(View.java:3599)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.view.View.performClick(View.java:4204)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.view.View$PerformClick.run(View.java:17360)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.os.Handler.handleCallback(Handler.java:725)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.os.Looper.loop(Looper.java:137)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.app.ActivityThread.main(ActivityThread.java:5233)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at java.lang.reflect.Method.invoke(Method.java:511)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at dalvik.system.NativeStart.main(Native Method)
08-04 15:04:05.599: E/AndroidRuntime(23230): Caused by: java.lang.reflect.InvocationTargetException
08-04 15:04:05.599: E/AndroidRuntime(23230):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at java.lang.reflect.Method.invoke(Method.java:511)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.view.View$1.onClick(View.java:3594)
08-04 15:04:05.599: E/AndroidRuntime(23230):    ... 11 more
08-04 15:04:05.599: E/AndroidRuntime(23230): Caused by: java.lang.NullPointerException
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.app.Activity.startActivityForResult(Activity.java:3381)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.app.Activity.startActivityForResult(Activity.java:3342)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.app.Activity.startActivity(Activity.java:3577)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at android.app.Activity.startActivity(Activity.java:3545)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at com.example.otpxmlgetter.Utils.planTripUtil(Utils.java:120)
08-04 15:04:05.599: E/AndroidRuntime(23230):    at com.example.otpxmlgetter.MainActivity.buttonCalls(MainActivity.java:202)

请检查您的utils的planTripUtil方法的代码,NullPointerException发生在120行中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM