NullPointerException when changing Activities
I'm pretty new to Android development, and I have a quick question about a NullPointerException
. When a button is pressed from the first activity page, it switches to the second activity, where there is a NullPointerException
.
Here's the code for the main.xml "next" button (the one that's pressed to switch activities):
<Button
android:id="@+id/ButtonNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/next"
android:onClick="next">
</Button>
Here's the code for the first activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class CalorieIntakeCalculator extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void next(View view) {
Intent intentExercise = new Intent(view.getContext(), Exercise.class);
startActivityForResult(intentExercise, 0);
}
}
When the "next" button is pressed, it switches to the exercise activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Exercise extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exercise);
final EditText weightField = (EditText) findViewById(R.id.EditTextWeight);
try {
((Global) this.getApplication()).setWeight(Integer.parseInt(weightField.getText().toString()));
开发者_StackOverflow中文版 } catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
In the exercise class, I am setting the ContentView to exercise.xml. In the next few statements, I want to set the global integer weight to the number that the user puts in the EditTextWeight EditText box. Here's the code in the Global.java file, which extends Appllication and makes the global variables:
import android.app.Application;
public class Global extends Application {
private int weight;
private int age;
private int feet;
private int inches;
//START WEIGHT
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
//END WEIGHT
//START AGE
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//END AGE
//START FEET
public int getFeet() {
return feet;
}
public void setFeet(int feet) {
this.feet = feet;
}
//END FEET
//START INCHES
public int getInches() {
return inches;
}
public void setInches(int inches) {
this.inches = inches;
}
//END INCHES
}
When I try and run the program in an emulator, the program crashes. I looked in LogCat and found the NullPointerException
using breakpoints in this line in exercise.java:
((Global) this.getApplication()).setWeight(Integer.parseInt(weightField.getText().toString()));
Thanks~ :)
If you want to preserve state across your application, I highly suggest looking into the SharedPrefrences
system.
From any Activity or Service in your Application, you can access the shared preferences by doing:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
You can also easily make a PreferencesActivity
to change the settings without having to write much code.
General tip for Null pointer Exceptions.
- Open the Debug perspective in Eclipse
- Choose the 'Breakpoints' view tab.
- Select the 'Add Java Exception Breakpoint' and choose NullPointerException.
- Launch your activity, either by using 'Debug As...' or attaching the debugger to a running instance via DDMS.
- Execute the offending workflow. It will break on the line that caused the NPE.
精彩评论