我看過這麼多的解決方案和應用所有,但仍然在我的代碼中得到空指針異常。這裏是我的代碼,請儘可能快地看看和回覆。提前致謝。在android中的數據庫保存查詢Java.lang.nullpointerexception
DBHandler.java類代碼
public class DBHandler extends SQLiteOpenHelper {
//Database Version
private static final int DATABASE_VERSION=1;
// Database Name
private static final String DATABASE_NAME="Patient.db";
//Table Name
private static final String TABLE_NAME="patientDetail";
//Table Columns Names
private static final String ID="id";
private static final String NAME="name";
private static final String ADDRESS="address";
private static final String PHONE_NUMBER="phone_number";
//Constructor
public DBHandler(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
//Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_PATIENT_DETAIL_TABLE="CREATE TABLE "+TABLE_NAME+"("
+ ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
+NAME+" TEXT,"
+ADDRESS +" TEXT,"
+PHONE_NUMBER + " TEXT)" ;
db.execSQL(CREATE_PATIENT_DETAIL_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop older tables if exist
db.execSQL("DROP TABLE IF EXIST "+TABLE_NAME);
/// Create Table again
onCreate(db);
}
public boolean insertData(String name,String address, String ph_no)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values= new ContentValues();
values.put(NAME,name);
values.put(ADDRESS,address);
values.put(PHONE_NUMBER,ph_no);
long result = db.insert(TABLE_NAME,null,values);
if(result == -1)
return false;
else
return true;
}
}
//insertPatient.java Class Code
public class insertPatient extends Fragment {
DBHandler mydb=null;
EditText editname,editaddress,editph_no;
Button btn_adddata;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.insert_patient, container, false);
mydb = new DBHandler(getActivity());
editname=(EditText) rootView.findViewById(R.id.insert_name);
editname=(EditText) rootView.findViewById(R.id.insert_address);
editname=(EditText) rootView.findViewById(R.id.insert_ph_no);
btn_adddata=(Button) rootView.findViewById(R.id.insert_button);
btn_adddata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adddata();
}
});
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydb = new DBHandler(getActivity());
}
public void adddata()
{
mydb = new DBHandler(getActivity());
Toast.makeText(getActivity(), "working button", Toast.LENGTH_LONG).show();
try {
boolean isinserted = mydb.insertData(editname.getText().toString(), editaddress.getText().toString(), editph_no.getText().toString());
if (isinserted) {
Toast.makeText(getActivity(), "data saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "data Not saved", Toast.LENGTH_LONG).show();
}
}
catch (Exception e) {
Toast.makeText(getActivity(), String.valueOf(e), Toast.LENGTH_LONG).show();
}
}
}
我爲dbhandler對象的initializtion appllied這麼多的解決方案,如任一按鈕被初始化與否,getactivity(),的getContext()和getbasecontext(),還提出了許多更改我的數據庫處理程序與不同的解決方案,但仍然有同樣的錯誤。我沒有檢查我的解決方案的活動,因爲我使用片段類,所以我只在它嘗試過。
提供堆棧跟蹤! –
請添加您的logcat請 –
我是新的數據庫,不知道如何添加logcat以及如何獲取堆棧跟蹤。謝謝 –