gpt4 book ai didi

java - 应用程序在 onTextChanged() 期间崩溃

转载 作者:行者123 更新时间:2023-12-01 21:26:27 26 4
gpt4 key购买 nike

我正在使用 SQLite 制作一个简单的笔记应用程序。它有两个 Activity 和一个 SQLite 帮助程序类。但是,当我单击 MainActivity.java 中的示例注释(Akhilesh Chobey)时,并尝试在 Main2Activity.java 中编辑它应用程序崩溃。

主要 Activity :

    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)

最佳答案

不初始化myDb = null;

Main2ActivityonCreate() 中像这样初始化 myDb

myDb = new DatabaseOperations(Main2Activity.this);

编辑

您现在已经初始化了您的myDb,并且它不会为空。因此,您必须在 onTextChanged() 方法中进行如下更改。

if (myDB == null)

if (myDB != null)

关于java - 应用程序在 onTextChanged() 期间崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com