在向我的数据库添加数据时,我有一个nullPointerExeption。通常我会认为我忘记了一个字符串,但EditTexts都填充了数据。 我发现我的DBHelper实例没有创建。但是我不知道我必须使用哪个命令来创建一个。
(this)和getActivity()/ getContext()都不起作用......
这是我的片段代码:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Button;
public class AddDataFragment extends Fragment {
DbHelper mydb;
Button buttonadd;
Button buttondelete;
private EditText inputProduct;
private EditText inputLabel;
private EditText inputSerial;
private EditText inputMac;
private EditText inputDaaId;
private EditText inputBill;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_data, container, false);
inputProduct = (EditText) view.findViewById(R.id.editText_product);
inputLabel = (EditText) view.findViewById(R.id.editText_label);
inputSerial = (EditText) view.findViewById(R.id.editText_serial);
inputMac = (EditText) view.findViewById(R.id.editText_mac);
inputDaaId = (EditText) view.findViewById(R.id.editText_daa_id);
inputBill = (EditText) view.findViewById(R.id.editText_bill);
buttonadd = (Button) view.findViewById(R.id.button_add_addData);
buttondelete = (Button) view.findViewById(R.id.button_delete_addData);
buttonadd.setEnabled(true);
buttondelete.setEnabled(true);
mydb = new DbHelper(getActivity());//////////////////////////////////////////////////////////////////////////////////////
buttonadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String product = inputProduct.getText().toString();
String label = inputLabel.getText().toString();
String serial = inputSerial.getText().toString();
String mac = inputMac.getText().toString();
String daaid = inputDaaId.getText().toString();
String bill = inputBill.getText().toString();
mydb.insertData(product, label, serial, mac, daaid, bill);
}
});
return view;
}
}
我的DbHelper课可能有问题。但我找不到......
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper{
public static final String DB_NAME = "DAAInv.db";
public static final int DB_VERSION = 1;
public static final String TABLE_LISTELEMENT = "listelement";
public static final String COLUMN_ID = "id";
public static final String COLUMN_PRODUCT = "product";
public static final String COLUMN_LABEL = "label";
public static final String COLUMN_SERIAL = "serial";
public static final String COLUMN_MAC = "mac";
public static final String COLUMN_DAAID = "daaid";
public static final String COLUMN_BILL = "bill";
public static final String SQL_CREATE =
"create table " + TABLE_LISTELEMENT + "(" +
COLUMN_ID + " integer primary key autoincrement, " +
COLUMN_PRODUCT + " text not null, " +
COLUMN_LABEL + " text not null, " +
COLUMN_SERIAL + " text not null, " +
COLUMN_MAC + " text not null, " +
COLUMN_DAAID + " text not null, " +
COLUMN_BILL + " text not null";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE); //Datenbanktable erstellen
ContentValues contentValues = new ContentValues();
contentValues.put("product", "PC");
contentValues.put("label", "WSN-1234");
contentValues.put("serial", "R4HSU74H");
contentValues.put("mac", "45:S8:LK:0L:5K");
contentValues.put("daaid", "145789");
contentValues.put("bill", "15.19.2015");
db.insert(TABLE_LISTELEMENT, null, contentValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public boolean insertData(String product, String label, String serial, String mac, String daaid, String bill) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("product", product);
contentValues.put("label", label);
contentValues.put("serial", serial);
contentValues.put("mac", mac);
contentValues.put("daaid", daaid);
contentValues.put("bill", bill);
db.insert(TABLE_LISTELEMENT, null, contentValues);
return true;
}
}
答案 0 :(得分:1)
您忘记创建db帮助程序的实例。
mydb.insertData(product, label, serial, mac, daaid, bill);
mydb为空