我正在尝试开始在 android 上创建我的第一个真正的练习应用程序,但遇到了一些麻烦。这是一个应用程序,它将以一种待办事项列表样式格式包含“想法”。我完成了将显示它的活动以及将添加想法的活动,但单击按钮后应用程序停止工作。调试后发现原因出在 onClick 方法中,具体是以下部分:
06-26 22:44:26.590 1136-1136/com.ivywire.ideastem E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.ivywire.ideastem.IdeasDbAdapter.createIdea(IdeasDbAdapter.java:78)
at com.ivywire.ideastem.IdeaAddActivity$1.onClick(IdeaAddActivity.java:44)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
所以我试着取出一些零件
Idea idea = new Idea(name, summary);
databaseHandler.createIdea(idea);
取出后它可以工作。这是完整的活动代码。
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class IdeaAddActivity extends Activity {
IdeasDbAdapter databaseHandler;
private EditText ideaName;
private EditText ideaSummary;
private Button addButton;
String name;
String summary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ideaadd);
ideaName = (EditText) findViewById(R.id.ideaName);
ideaSummary = (EditText) findViewById(R.id.ideaSummary);
addButton = (Button) findViewById(R.id.addIdeaButton);
name = ideaName.getText().toString();
summary = ideaSummary.getText().toString();
databaseHandler = new IdeasDbAdapter(this);
addButton.setOnClickListener(addListener);
}
private OnClickListener addListener = new OnClickListener(){
public void onClick(View view){
Idea idea = new Idea(name, summary);
databaseHandler.createIdea(idea);
sendToMain(view);
}
};
public void sendToMain(View view){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.idea_add, menu);
return true;
}
}
这是 IdeasDbAdapter 类:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class IdeasDbAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_SUMMARY = "summary";
private static final String TAG = "IdeasDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "Brainstorm";
private static final String SQLITE_TABLE = "Ideas";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_NAME + "," +
KEY_SUMMARY + ");";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public IdeasDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public IdeasDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createIdea(Idea idea) {
String name = idea.getName();
String summary = idea.getSummary();
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_SUMMARY, summary);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllIdeas() {
int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchIdeasByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_SUMMARY},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_SUMMARY},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllIdeas() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_SUMMARY},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
空指针异常中引用的第 78 行是
return mDb.insert(SQLITE_TABLE, null, initialValues);
如果有人可以给我提示,因为我很困惑并尽我所能进行搜索,我将不胜感激。