仅在实际手机(不是模拟器)上运行时出错

时间:2015-02-26 18:54:50

标签: android android-emulator

在Android Studio中制作我的应用程序,我一直在模拟器上运行/测试它。好吧,我已经完成了它(我想),我在我的手机上运行它,我之前已经做过很多。现在,当我参加我的一项活动时,我应该选择一些问题(测验应用程序)进行我想要的练习测试,并点击“开始”。在我的实际手机上,这会发送一个错误,并显示一条消息,该应用已不幸停止工作。该应用程序然后重置。

       02-26 10:42:16.458      964-964/godwin.com.study E/AndroidRuntime﹕ FATAL EXCEPTION: main
       Process: godwin.com.study, PID: 964
        java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3999)
        at android.view.View.performClick(View.java:4753)
        at android.view.View$PerformClick.run(View.java:19562)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        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:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3994)
        at android.view.View.performClick(View.java:4753)
        at android.view.View$PerformClick.run(View.java:19562)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        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:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at android.content.Intent.putExtra(Intent.java:6193)
        at godwin.com.study.chooseTestLength.sendLengthOnClick(chooseTestLength.java:57)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3994)
        at android.view.View.performClick(View.java:4753)
        at android.view.View$PerformClick.run(View.java:19562)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        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:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

这是按下按钮时崩溃的java / activity文件:

package godwin.com.study;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Spinner;


public class chooseTestLength extends ActionBarActivity {

public static String TEST_LENGTH;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_test_length);
}




public void sendLengthOnClick (View view){

    //Create new activity
    Intent intent = new Intent(this, takeTest.class);

    //Get spinner selection
    final Spinner choiceSpinner = (Spinner) findViewById(R.id.testLengthSpinner);
    int num = Integer.parseInt(choiceSpinner.getSelectedItem().toString());

    intent.putExtra(TEST_LENGTH, num);
    startActivity(intent);
}


}

这是XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@color/background"
tools:context="godwin.com.study.chooseTestLength">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true">

    <TextView
        android:id="@+id/chooseLength"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chooseLengthText"
        android:layout_gravity="center"/>


    <Spinner
        android:id="@+id/testLengthSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/testLength"
        android:entries="@array/testLengthChoice"
        android:layout_gravity="center"
        android:padding="10dp"/>


    <Button
        android:id="@+id/buttonSendLength"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/startTest"
        android:background="@color/primary"
        android:onClick="sendLengthOnClick"
        android:layout_gravity="center"/>

</LinearLayout>



</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

看起来你要么在第57行的Intent.putExtra()函数调用的godwin.com.study.chooseTestLength.sendLengthOnClick()方法中传入NULL值,要么Intent本身为NULL。请发布一些代码以更好地评估问题。

编辑:

如前所述,TEST_LENGTH为NULL。给它一个像这样的值:

public static String TEST_LENGTH = "TEST_LENGTH";

Intent extras用作键/值对。 TEST_LENGTH是你的密钥,num是你的值。键不能为NULL。取决于对象类型,值有时可以为NULL。