我试图在我的Db中插入一些记录。问题是当我点击保存按钮时我得到了这个例外。所以我首先检查了logcat并过滤掉了这些消息。
03-21 18:07:00.820: E/AndroidRuntime(4756): FATAL EXCEPTION: main
03-21 18:07:00.820: E/AndroidRuntime(4756): java.lang.NullPointerException
03-21 18:07:00.820: E/AndroidRuntime(4756): at com.example.kjk.MainActivity$1.onClick(MainActivity.java:51)
03-21 18:07:00.820: E/AndroidRuntime(4756): at android.view.View.performClick(View.java:4240)
03-21 18:07:00.820: E/AndroidRuntime(4756): at android.view.View$PerformClick.run(View.java:17721)
03-21 18:07:00.820: E/AndroidRuntime(4756): at android.os.Handler.handleCallback(Handler.java:730)
03-21 18:07:00.820: E/AndroidRuntime(4756): at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 18:07:00.820: E/AndroidRuntime(4756): at android.os.Looper.loop(Looper.java:137)
03-21 18:07:00.820: E/AndroidRuntime(4756): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 18:07:00.820: E/AndroidRuntime(4756): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 18:07:00.820: E/AndroidRuntime(4756): at java.lang.reflect.Method.invoke(Method.java:525)
03-21 18:07:00.820: E/AndroidRuntime(4756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 18:07:00.820: E/AndroidRuntime(4756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 18:07:00.820: E/AndroidRuntime(4756): at dalvik.system.NativeStart.main(Native Method)
我认为异常来自这里然而我无法找到解决问题的任何解决方案
AddData(name.getText()。toString(),surname.getText()。toString());
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.edtName);
surname=(EditText)findViewById(R.id.edtsurname);
btnsave=(Button)findViewById(R.id.btnSave);
btnfetch=(Button)findViewById(R.id.btnFetch);
tvname=(TextView)findViewById(R.id.tvNmae);
tvsurname=(TextView)findViewById(R.id.tvSurname);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
AddData(name.getText().toString(), surname.getText().toString());
} catch (Exception e) {
Log.e("Error ", e.getMessage());
}
finally{
v1.close();
}
}
});
btnfetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=v1.getReadableDatabase();
Cursor cursor=db.query( "Info", columns, null, null, null, null, null);
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String surname=cursor.getString(cursor.getColumnIndex("surname"));
tvname.setText(name);
tvsurname.setText(surname);
}
}
});
}
private void AddData(String name,String sur) {
SQLiteDatabase db=v1.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(name, sur);
db.insertOrThrow("Info", null, cv);
}
以下是数据库类
private static final String Db_Name="Mydb";
private static final int ver=1;
public Veritabani(Context context, String name, CursorFactory factory,
int version) {
super(context, Db_Name, null, ver);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Info(name TEXT,surname TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST Info");
onCreate(db);
}
答案 0 :(得分:0)
您的堆栈跟踪说
java.lang.NullPointerException
03-21 18:07:00.820: E/AndroidRuntime(4756):at com.example.kjk.MainActivity$1.onClick(MainActivity.java:51)
来自您的评论
所以v1是数据库和第51行的实例,表示为 logcat是v1.close();
这表示v1为空,并且您尚未正确初始化它。
试
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v1 = new Veritabani(this);
答案 1 :(得分:0)
尝试将cursor.moveToFirst()
放在while(cursor.moveToNext()){}
代码中的btnfetch.setOnClickListener
之前。