我在调用getWritableDatabase()时遇到麻烦。它返回一个NullPointerException。最奇怪的是,此错误仅在我注释掉了Android清单中的几行代码后才开始发生。现在,项目返回到原来的位置(我未注释这些部分,并且仍然抛出NullPointerException)。
这是sq-lite开放式文件,它按片段访问。
package com.example.cartracking;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBLogic extends SQLiteOpenHelper {
public static String Lock = "dblock";
private static final String DATABASE_NAME="cartracking.db";
private static final int DATABASE_VERSION=1;
private static final String TABLE_NAME="tb_customer";
public static final String FULL_NAME = "fullname";
public static final String MOBILE_NO = "mobileno";
public static final String ADDRESS = "address";
public static final String HOUSE_NO = "houseno";
public static final String STREET_NO = "streetno";
public static final String PIN_CODE = "pincode";
private SQLiteDatabase database;
public DBLogic(Context context) {
super(context,DATABASE_NAME , null, DATABASE_VERSION);
}
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "(" + FULL_NAME
+ " text not null," + MOBILE_NO + " text not null," + ADDRESS + " text not null,"
+ HOUSE_NO + " text null," + STREET_NO
+ " text null," + PIN_CODE + " text null" + ")";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String Query1="DROP TABLE IF EXISTS"+TABLE_NAME;
db.execSQL(Query1);
onCreate(db);
}
public void openDB() throws SQLException{
synchronized(Lock) {
Log.e("Hello", "Hi");
database=getWritableDatabase();
}
}
public void closeDB(){
synchronized(Lock) {
database.close();
}
}
//protected void onHandleIntent(Intent intent) {
// dbHelper = new TasksDBAdapter(this);
// database.open();
// Do something
//}
public long insertQuery(ContentValues cValues){
return database.insert(TABLE_NAME, null, cValues);
}
public int updateQuery(ContentValues cValues){
String WHERE="_id="+cValues.getAsString("_id");
return database.update(TABLE_NAME, cValues, WHERE, null);
}
public int deleteQuery(String id){
String WHERE="_id="+id;
return database.delete(TABLE_NAME, WHERE, null);
}
public Cursor getString(String[] colName){
Cursor cursor=database.query(TABLE_NAME, colName, null, null, null, null, null);
return cursor;
}
}
可以访问sq-lite openhanded文件的另一个Java文件。
package com.example.cartracking;
import android.app.Fragment;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CustomerRegister extends Fragment {
private EditText fulname,mobile,address,house_no,street_no,pincode;
private Button clear,registration;
String fullName,contact,adr,houseNo,streetNo,PinCode;
private final DBLogic logic=new DBLogic(getActivity());
public CustomerRegister(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.customer_register, container, false);
logic.openDB();
Button reset = (Button)rootView.findViewById(R.id.btn2);
fulname=(EditText)rootView.findViewById(R.id.editText7);
mobile=(EditText)rootView.findViewById(R.id.editText8);
address=(EditText)rootView.findViewById(R.id.editText9);
house_no=(EditText)rootView.findViewById(R.id.editText10);
street_no=(EditText)rootView.findViewById(R.id.editText11);
pincode=(EditText)rootView.findViewById(R.id.editText12);
Button registration = (Button)rootView.findViewById(R.id.btn1);
registration.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
fullName=fulname.getText().toString();
contact=mobile.getText().toString();
adr=address.getText().toString();
houseNo=house_no.getText().toString();
streetNo=street_no.getText().toString();
PinCode=pincode.getText().toString();
String columns[]={"full_name","mobile_no","address","house_no","steet_no","pin_code"};
String colValues[]={fullName,contact,adr,houseNo,streetNo,PinCode};
ContentValues cValues=new ContentValues();
for(int i=0;i<columns.length;i++){
cValues.put(columns[i], colValues[i]);
}
try{
long id=logic.insertQuery(cValues);
Toast.makeText(getActivity(), "Row Id is:"+id, 0).show();
}
catch(Exception e){
Toast.makeText(getActivity(), "Error", 0).show();
}
}
});
reset.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
引发空指针错误。
Logcat提供-
05-04 01:08:22.870: E/Hello(1705): Hi
05-04 01:08:22.900: E/AndroidRuntime(1705): FATAL EXCEPTION: main
05-04 01:08:22.900: E/AndroidRuntime(1705): Process: com.example.cartracking, PID: 1705
05-04 01:08:22.900: E/AndroidRuntime(1705): java.lang.NullPointerException
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-04 01:08:22.900: E/AndroidRuntime(1705): at com.example.cartracking.DBLogic.openDB(DBLogic.java:56)
05-04 01:08:22.900: E/AndroidRuntime(1705): at com.example.cartracking.CustomerRegister.onCreateView(CustomerRegister.java:27)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.Fragment.performCreateView(Fragment.java:1700)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.os.Handler.handleCallback(Handler.java:733)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.os.Handler.dispatchMessage(Handler.java:95)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.os.Looper.loop(Looper.java:136)
05-04 01:08:22.900: E/AndroidRuntime(1705): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-04 01:08:22.900: E/AndroidRuntime(1705): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 01:08:22.900: E/AndroidRuntime(1705): at java.lang.reflect.Method.invoke(Method.java:515)
05-04 01:08:22.900: E/AndroidRuntime(1705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-04 01:08:22.900: E/AndroidRuntime(1705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-04 01:08:22.900: E/AndroidRuntime(1705): at dalvik.system.NativeStart.main(Native Method)
最佳答案
传递给数据库帮助器的Context
为空。实例化片段时,片段未附加到活动,并且getActivity()
返回null。将db helper初始化移至片段生命周期的后期,例如onCreate()
。
关于android - getWritableDatabase()在我的应用中引发空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23449384/