lilgodwin lilgodwin - 2 months ago 11
Android Question

Error only when running on actual phone (not emulator)

Making my app in Android Studio, I've been mostly running/testing it on the emulator. Well I'm pretty much done with it(I think), and I ran it on my phone, which I've done before much earlier on. Now when I get to one of my activities, I'm supposed to select a number of questions (quiz app) for a practice test I want to take, and hit "begin". On my actual phone, this sends an error and brings up a message that the app has unfortunately stopped working. The app then resets.

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)


Here's the java/activity file that crashes when the button is pressed:

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


}


Here's the XML file:

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

Answer Source

Looks like you're either passing in a NULL value in the Intent.putExtra() method called from the godwin.com.study.chooseTestLength.sendLengthOnClick() function on line 57 or the Intent itself is NULL. Please post some code to better assess the issue.

EDIT:

As already stated, TEST_LENGTH is NULL. Give it a value like this:

public static String TEST_LENGTH = "TEST_LENGTH";

Intent extras work as key/value pairs. TEST_LENGTH is your key, and num is your value. Keys can not be NULL. Values can sometimes be NULL depending on the object type.