我正在尝试开始在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;
}
}
nullpointer异常中引用的第78行是
return mDb.insert(SQLITE_TABLE, null, initialValues);
如果有人能给我提示,因为我很困惑并尽可能地搜索,我会很感激。
答案 0 :(得分:0)
我不知道你的其他代码上的字符串 但看看我的代码,看看你要做什么
addactivity:
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
memo = new Memo();
memo.setHeadLine(headLineTxt.getText().toString());
memo.setFullMemo(fullMemoTxt.getText().toString());
memo = datasource.createMemo(memo);
finish();
}
});
IdeasDbAdapter:
public Memo createMemo(Memo memo) {
//Used to store data like : COLUMN_NAME , VALUE.
ContentValues values = new ContentValues();
values.put(COLUMN_HEAD_LINE, memo.getHeadLine());
values.put(COLUMN_FULL_MEMO, memo.getFullMemo());
//Insert new memo and get the id of it.
long insertId = database.insert(TABLE_NAME, null,
values);
我希望它对你有所帮助。
答案 1 :(得分:-1)
我正在寻找一个很好的教程,解释如何为它创建一个好的SQLite数据库和内容提供程序。我找到了这个,我认为如果你遵循它,你将拥有一个良好的架构,访问,删除或升级你的数据没有问题:
http://www.vogella.com/articles/AndroidSQLite/article.html#todo