SimpleAdapter 每隔一秒启动一次就崩溃一次
Posted
技术标签:
【中文标题】SimpleAdapter 每隔一秒启动一次就崩溃一次【英文标题】:SimpleAdapter crashes every second launch time 【发布时间】:2017-06-20 00:43:21 【问题描述】:我有一个 SimpleAdapter 过滤 inputSearch 和可点击的项目(我从 mysql 数据库中获得)。
有时在我的项目午餐时出现错误:
java.lang.RuntimeException: Unable to start activity ComponentInfocom.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)
当我下次启动应用程序时,一切正常。 所以错误发生在每第二次启动。
【问题讨论】:
可以输入你的ChooseGroupActivity的代码吗? 我添加了代码。 【参考方案1】:从 onResume 中移除 simpleAdapter 初始化。 在你的 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);
【讨论】:
【参考方案2】:您在onResume
中创建了SimpleAdapter
,但这里发生的是您在onCreate
中设置了textChangedListener。调用onCreate
后,Android 会恢复视图状态,包括视图中的文本。这会在 onResume
运行之前触发文本更改侦听器并创建 SimpleAdapter
。我认为你可以将SimpleAdapter
设置移动到onCreate
,然后它应该就可以工作了。
【讨论】:
以上是关于SimpleAdapter 每隔一秒启动一次就崩溃一次的主要内容,如果未能解决你的问题,请参考以下文章
求高手赐教: 想做一个用CPU定时器0的中断,产生一个每隔一秒LED灯闪烁一次的程序