【发布时间】:2014-03-20 07:43:12
【问题描述】:
我正在制作一个笔记应用程序,我在其中做笔记,它被存储在 Database 中,我可以编辑和删除它,所以,我正要继续我的编辑和删除冒险,我以为我会运行应用程序,看看它是否还好,它刚刚崩溃了。
数据库处理程序
package com.example.quicknotetaker;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Qdatabase";
// Contacts table name
private static final String DATABASE_TABLE = "qtable";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_NOTE = "note";
private final ArrayList<Editablegetset> titlenoteslist = new ArrayList<Editablegetset>();
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_DATABASE_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
+ KEY_NOTE + " TEXT,)";
db.execSQL(CREATE_DATABASE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
public void Add_titlenotes(Editablegetset editablegetset) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, editablegetset.getTitle()); // Contact Name
values.put(KEY_NOTE, editablegetset.getNote()); // Contact Phone
// Inserting Row
db.insert(DATABASE_TABLE, null, values);
db.close(); // Closing database connection
}
// Getting single data
Editablegetset Get_Set(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DATABASE_TABLE, new String[] { KEY_ID,
KEY_TITLE, KEY_NOTE}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Editablegetset editablegetset = new Editablegetset(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
cursor.close();
db.close();
return editablegetset;
}
// Getting All Contacts
public ArrayList<Editablegetset> Get_Set() {
try {
titlenoteslist.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + DATABASE_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Editablegetset editablegetset = new Editablegetset();
editablegetset.setID(Integer.parseInt(cursor.getString(0)));
editablegetset.setTitle(cursor.getString(1));
editablegetset.setNote(cursor.getString(2));
// Adding to list
titlenoteslist.add(editablegetset);
} while (cursor.moveToNext());
}
// return list
cursor.close();
db.close();
return titlenoteslist;
} catch (Exception e) {
// TODO: handle exception
Log.e("all title and notes", "" + e);
}
return titlenoteslist;
}
// Updating individual notes
public int Update_Contact(Editablegetset editablegetset) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, editablegetset.getTitle());
values.put(KEY_NOTE, editablegetset.getNote());
// updating row
return db.update(DATABASE_TABLE, values, KEY_ID + " = ?",
new String[] { String.valueOf(editablegetset.getID()) });
}
// Deleting single notes
public void Delete_Contact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DATABASE_TABLE, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
// Getting notes Count
public int totalnotes() {
String countQuery = "SELECT * FROM " + DATABASE_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
这是我的主要活动
package com.example.quicknotetaker;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Mainnote extends Activity {
DatabaseHandler db = new DatabaseHandler(this);
EditText edtitle, enotes;
Button ab;
int noteid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainnote);
edtitle = (EditText) findViewById(R.id.title);
enotes = (EditText) findViewById(R.id.notes);
ab = (Button)findViewById(R.id.addnote);
ab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Editablegetset ed = db.Get_Set(noteid);
edtitle.setText(ed.getTitle());
enotes.setText(ed.getNote());
Toast.makeText(getApplicationContext(),
"Note has been Added", Toast.LENGTH_LONG).show();
Clear_Text();
}
public void Clear_Text() {
// TODO Auto-generated method stub
edtitle.getText().clear();
enotes.getText().clear();
}
});
}
}
这是我的get set方法
package com.example.quicknotetaker;
public class Editablegetset {
// private variables
public int _id;
public String _title;
public String _note;
public Editablegetset() {
}
// constructor
public Editablegetset(int id, String title, String note) {
this._id = id;
this._title = title;
this._note = note;
}
// constructor
public Editablegetset(String title, String note) {
this._title = title;
this._note = note;
}
// getting ID
public int getID() {
return this._id;
}
// setting id
public void setID(int id) {
this._id = id;
}
// getting title
public String getTitle() {
return this._title;
}
// setting title
public void setTitle(String title) {
this._title = title;
}
// getting note
public String getNote() {
return this._note;
}
// setting note
public void setNote(String note) {
this._note = note;
}
}
日志猫
03-20 07:48:37.458: D/AndroidRuntime(23951): Shutting down VM
03-20 07:48:37.458: W/dalvikvm(23951): threadid=1: thread exiting with uncaught exception (group=0x4170ad40)
03-20 07:48:37.460: E/AndroidRuntime(23951): FATAL EXCEPTION: main
03-20 07:48:37.460: E/AndroidRuntime(23951): Process: com.example.quicknotetaker, PID: 23951
03-20 07:48:37.460: E/AndroidRuntime(23951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quicknotetaker/com.example.quicknotetaker.Mainnote}: java.lang.NullPointerException
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.os.Looper.loop(Looper.java:136)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.ActivityThread.main(ActivityThread.java:5102)
03-20 07:48:37.460: E/AndroidRuntime(23951): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 07:48:37.460: E/AndroidRuntime(23951): at java.lang.reflect.Method.invoke(Method.java:515)
03-20 07:48:37.460: E/AndroidRuntime(23951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-20 07:48:37.460: E/AndroidRuntime(23951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-20 07:48:37.460: E/AndroidRuntime(23951): at dalvik.system.NativeStart.main(Native Method)
03-20 07:48:37.460: E/AndroidRuntime(23951): Caused by: java.lang.NullPointerException
03-20 07:48:37.460: E/AndroidRuntime(23951): at com.example.quicknotetaker.Mainnote.onCreate(Mainnote.java:30)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.Activity.performCreate(Activity.java:5248)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
03-20 07:48:37.460: E/AndroidRuntime(23951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
03-20 07:48:37.460: E/AndroidRuntime(23951): ... 11 more
拜托我真的需要帮助我花了很多时间在这谢谢
【问题讨论】:
-
例外是....?
-
当我在我的安卓手机上运行它时,它只是说 - 不幸的是,快速笔记记录已停止。
-
您需要从 logcat 发布堆栈跟踪。这将准确显示错误的位置。
-
我现在已经添加了logcat
-
由于
onCreate()中存在 NPE,因此布局可能没有 ID 为addnote的视图。
标签: java android eclipse sqlite