0

我的代码中有这个设置。

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" />

主要活动

    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;
    }

}

实用程序活动

    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);

}

每当我单击按钮时,我都会收到此错误。我知道问题出在 startActivity(intent) 上,它每次都会给我一个 IllegalStateException。任何想法为什么以及如何解决这个问题?

    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)
4

1 回答 1

0

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

于 2013-08-04T07:51:30.590 回答