gpt4 book ai didi

android - SimpleAdapter 每秒崩溃一次

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:06 27 4
gpt4 key购买 nike

我有一个带有过滤 inputSearch 和可点击项(我从 MySQL 数据库中获取)的 SimpleAdapter。

有时当我吃午饭时我的项目会出现错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brzozowski.marcin.inzynierka/com.brzozowski.marcin.inzynierka.activities.ChooseGroupActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.SimpleAdapter.getFilter()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.SimpleAdapter.getFilter()' on a null object reference
at com.brzozowski.marcin.inzynierka.activities.ChooseGroupActivity$2.onTextChanged(ChooseGroupActivity.java:103)
at android.widget.TextView.sendOnTextChanged(TextView.java:7679)
at android.widget.TextView.setText(TextView.java:4060)
at android.widget.TextView.setText(TextView.java:3915)
at android.widget.EditText.setText(EditText.java:85)
at android.widget.TextView.setText(TextView.java:3890)
at android.widget.TextView.onRestoreInstanceState(TextView.java:3790)
at android.view.View.dispatchRestoreInstanceState(View.java:13740)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
at android.view.View.restoreHierarchyState(View.java:13718)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2009)
at android.app.Activity.onRestoreInstanceState(Activity.java:1023)
at android.app.ListActivity.onRestoreInstanceState(ListActivity.java:219)
at android.app.Activity.performRestoreInstanceState(Activity.java:978)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1162)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947) 
at android.app.ActivityThread.access$900(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

当我下次启动该应用程序时,一切正常。因此,错误会在每次启动时发生。

最佳答案

从 onResume 中删除简单的适配器初始化。在你的 onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initAdapter();

}
private void initAdapter(){
simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "groups" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(simpleAdapter == null){
initAdapter();
}
simpleAdapter.getFilter().filter(s);
}

关于android - SimpleAdapter 每秒崩溃一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42010783/

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