当我打开我的数据库打开的活动以及其他一些东西时,我的应用程序崩溃并出现NullPointerException。
我按下一个打开此活动的按钮WorkoutEdit.java,它立即崩溃。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class WorkoutEdit extends Activity {
public TextView mTitleText;
//private EditText mBodyText;
private Long mRowId;
private StrongDbAdapter mDbHelper;
private ViewFlipper viewFlipper;
public TextView squats;
public boolean workoutA;
public String workoutState;
//private double rowId = mRowId;
public String squatLabel;
public Button confirmButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_edit3);
mDbHelper = new StrongDbAdapter(this);
mDbHelper.open();
setTitle(R.string.edit_workout);
mTitleText = (TextView) findViewById(R.id.workout_row);
//mBodyText = (EditText) findViewById(R.id.body);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
squats = (TextView) findViewById(R.id.squatLabel);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
squatLabel = squats.getText().toString();
confirmButton = (Button) findViewById(R.id.confirmButton);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(StrongDbAdapter.KEY_ROWID);
if (mRowId == null) {
getWorkoutState();
if(workoutA == true){
workoutState = "WorkoutA";
}else{
workoutState = "WorkoutB";
}
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(StrongDbAdapter.KEY_ROWID)
: null;
}
// populateFields();
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
squats.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
alert.setTitle("Squats");
alert.setMessage("Message");
//AlertDialog dialog = alert.create();
//set an EditText view to get user input
final EditText input = new EditText(aMethod());
input.setInputType(InputType.TYPE_CLASS_NUMBER);
//dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
String value = input.getText().toString();
//Do something with value
value = "Squats - " + value;
squats.setText(value);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
//Cancelled
}
});
AlertDialog dialog = alert.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//alert.show();
dialog.show();
}
});
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radgroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case(R.id.rad2):
viewFlipper.setDisplayedChild(1);
break;
case(R.id.rad3):
viewFlipper.setDisplayedChild(2);
break;
default:
viewFlipper.setDisplayedChild(0);
break;
}
}
});
//saveState();
if((Long)(mRowId-1) == null){
squats.setText("Squats - 45");
}else{
Cursor note = mDbHelper.fetchNote((Long)(mRowId-1));
startManagingCursor(note);
squats.setText(note.getString(
note.getColumnIndexOrThrow(StrongDbAdapter.SQUAT_LABEL)));
String squat = squats.getText().toString();
int squatsWeight = Integer.parseInt(squat);
squatsWeight = squatsWeight + 5 ;
squats.setText("Squats - " + squatsWeight);
}
//end of onCreate
}
/*
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(StrongDbAdapter.KEY_TITLE)));
workoutState=(note.getString(
note.getColumnIndexOrThrow(StrongDbAdapter.WORKOUT_STATE)));
squats.setText(note.getString(note.getColumnIndexOrThrow(StrongDbAdapter.SQUAT_LABEL)));
}
}
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//saveState();
//outState.putSerializable(StrongDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
//saveState();
}
@Override
protected void onResume() {
super.onResume();
//populateFields();
}
//Saves all the data to the database
private void saveState() {
String title = workoutState;
String squatLabel = squats.getText().toString();
if (mRowId == null) {
long id = mDbHelper.createNote(title, squatLabel);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title);
}
}
public Context aMethod(){
Context actContext = this;
return actContext;
}
public boolean getWorkoutState(){
if(workoutA!=false){
workoutA = true;
}else{
workoutA = false;
}
return workoutA;
}
}
以下是我的LogCat日志:
08-04 20:12:27.682: E/AndroidRuntime(23360): FATAL EXCEPTION: main
08-04 20:12:27.682: E/AndroidRuntime(23360): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anapoleon.android.stronglifts/com.anapoleon.android.stronglifts.WorkoutEdit}: java.lang.NullPointerException
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.os.Handler.dispatchMessage(Handler.java)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.os.Looper.loop(Looper.java)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.ActivityThread.main(ActivityThread.java:4385)
08-04 20:12:27.682: E/AndroidRuntime(23360): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 20:12:27.682: E/AndroidRuntime(23360): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 20:12:27.682: E/AndroidRuntime(23360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
08-04 20:12:27.682: E/AndroidRuntime(23360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
08-04 20:12:27.682: E/AndroidRuntime(23360): at dalvik.system.NativeStart.main(Native Method)
08-04 20:12:27.682: E/AndroidRuntime(23360): Caused by: java.lang.NullPointerException
08-04 20:12:27.682: E/AndroidRuntime(23360): at com.anapoleon.android.stronglifts.WorkoutEdit.onCreate(WorkoutEdit.java:166)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
08-04 20:12:27.682: E/AndroidRuntime(23360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
08-04 20:12:27.682: E/AndroidRuntime(23360): ... 11 more
答案 0 :(得分:0)
所以...你的mRowId可以在你的代码中设置为null,但你不会在第166行检查它的null状态。
另请注意(长)(mRowId-1)== null永远不会成真。
答案 1 :(得分:0)
你在第166行的情况没有任何意义:
(Long)(mRowId-1) == null
如果 mRowid 不为空,那么(Long)(mRowId-1)
永远不会 null ,但如果 mRowid null ,然后您将收到NullPointerException,因为null - 1
不正确。
所以你不会得到任何类型的
当前mRowId之前的ID
你可能想要使用其他一些结构。