我正在用SQLite制作一个简单的笔记应用程序。它有两个活动和一个SQLite的帮助器类。但是,当我在MainActivity.java
中单击示例注释(Akhilesh Chobey)并尝试在Main2Activity.java
中编辑它时,应用程序崩溃。
主要活动:
public class MainActivity extends AppCompatActivity {
ListView notesListView;
static ArrayList<String> notesArrayList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notesListView = (ListView) findViewById(R.id.notesListView);
notesArrayList = new ArrayList<String>();
notesArrayList.add("Akhilesh Chobey");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
notesListView.setAdapter(adapter);
notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("notePosition", i);
startActivity(intent);
}
});
}
}
Main2Activity:
public class Main2Activity extends AppCompatActivity implements TextWatcher {
DatabaseOperations myDB;
EditText editNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editNote = (EditText) findViewById(R.id.noteEditText);
myDb = new DatabaseOperations(Main2Activity.this);
Intent intent = getIntent();
int position = intent.getIntExtra("notePosition", -1);
if(position != -1){
editNote.setText(MainActivity.notesArrayList.get(position));
}
editNote.addTextChangedListener(this);
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (myDB == null){
boolean isInserted = myDB.insertData(editNote.getText().toString());
if(isInserted){
Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}
助手班级:
public class DatabaseOperations extends SQLiteOpenHelper {
public static final String DatabaseName = "notes.db";
public static final String TableName = "notes";
public static final String Col1 = "text";
public DatabaseOperations(Context context) {
super(context, DatabaseName, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TableName + " (text TEXT PRIMARY KEY AUTOINCREMENT) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TableName);
onCreate(db);
}
public boolean insertData(String note){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col1, note);
long result = db.insert(TableName, null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}
}
日志:
06-29 17:23:34.996 2231-2231/com.akhileshchobey.mynotes E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.akhileshchobey.mynotes, PID: 2231
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.akhileshchobey.mynotes.DatabaseOperations.insertData(java.lang.String)' on a null object reference
at com.akhileshchobey.mynotes.Main2Activity.onTextChanged(Main2Activity.java:39)
at android.widget.TextView.sendOnTextChanged(TextView.java:7746)
at android.widget.TextView.handleTextChanged(TextView.java:7806)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9724)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:445)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
答案 0 :(得分:2)
不要初始化myDb
,
在您onCreate()
的{{1}}
Main2Activity
myDb = new DatabaseOperations(Main2Activity.this);
修改强>
您现在已经初始化了myDb
,它不会为空。因此,您必须在onTextChanged()
方法中进行如下更改。
if (myDB == null)
到
if (myDB != null)
答案 1 :(得分:1)
使用安全空检查更改行boolean isInserted = myDB.insertData(editNote.getText().toString());
,如下所示。
if (myDB == null)
myDB = new DatabaseOperations(getApplicationContext() ); //Initialization
boolean isInserted = myDB.insertData(editNote.getText().toString());
答案 2 :(得分:0)
您尚未初始化DatabaseOperations myDB = null;
以下行必须给出空指针异常;
boolean isInserted = myDB.insertData(editNote.getText().toString());
因为您尚未初始化myDB
答案 3 :(得分:0)
myDB
未初始化。您需要在使用前初始化它。在null对象上调用insertData
是您的应用崩溃的原因。