gpt4 book ai didi

java - NullPointerException - 尝试调用虚拟方法 `notifyDataSetChanged()`

转载 作者:行者123 更新时间:2023-12-01 09:44:42 25 4
gpt4 key购买 nike

在标记此重复项之前,我确实查看了其他类似问题的解决方案,但它不起作用。仅当我第一次运行我的应用程序时,我才收到此错误。我不知道为什么会发生这种情况。我打电话MainActivity.adapter.notifyDataSetChangedMain2Activity (编辑文本 Activity )。安装后在 EditText Activity 中按后退按钮时应用程序崩溃,即 Main2Activity.java .

主要 Activity :

public class MainActivity extends AppCompatActivity {

ListView notesListView;
static ArrayList<String> notesArrayList;
static 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>();

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
notesListView.setAdapter(adapter);
Main2Activity.myDb = new DatabaseOperations(this);

if (Main2Activity.myDb != null) {
notesArrayList.clear();
Cursor result = Main2Activity.myDb.getData();
if (result.getCount() == 0) {
Log.i("Error", "error");
return;
}
while (result.moveToNext()) {
notesArrayList.add(result.getString(result.getColumnIndex("text")));
}
}

//notesArrayList.add("Akhilesh Chobey");

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);
}
});

notesListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {


return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.add_button) {
notesArrayList.add("");
}
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("notePosition", notesArrayList.size() - 1);
startActivity(intent);


return true;

}
}

Main2Activity(EditText Activity ):

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

@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();
position = intent.getIntExtra("notePosition", -1);
if(position != -1){

editNote.setText(MainActivity.notesArrayList.get(position));

}
editNote.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (myDb != null) {

}

}

@Override
public void afterTextChanged(Editable editable) {

}

@Override
public void onBackPressed(){
myDb.insertData(editNote.getText().toString());
MainActivity.notesArrayList.set(position, editNote.getText().toString());
MainActivity.adapter.notifyDataSetChanged();
this.finish();
}
}

日志:

07-02 18:45:40.402 17500-17500/com.akhileshchobey.mynotes E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference at com.akhileshchobey.mynotes.Main2Activity.onBackPressed(Main2Activity.java:70) at android.app.Activity.onKeyUp(Activity.java:2495) at android.view.KeyEvent.dispatch(KeyEvent.java:2695) at android.app.Activity.dispatchKeyEvent(Activity.java:2746) at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:533) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53) at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:254) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2221) at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3918) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3880) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611) at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3772) at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2244) at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1885) at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1876) at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2221) at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122) 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)

最佳答案

ArrayAdapter.notifyDataSetChanged() 已被调用,但 ArrayAdapter 实例指向空引用(未初始化或错误初始化...)

在你的情况下,看起来第二个选项是原因......

代码中有这样的内容:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);

但需要检查构造函数中的参数实例:这个布局中的资源:android.R.layout.simple_list_item_1 或列表:notesArrayList

其中至少有一个破坏了适配器的正确初始化,这意味着,在该线路适配器之后仍然是null

结果是:调用adapter.notifyDataSetChanged()会抛出NPE

关于java - NullPointerException - 尝试调用虚拟方法 `notifyDataSetChanged()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38160621/

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