SimpleAdapter每秒都会崩溃

时间:2017-02-02 19:31:26

标签: android mysql simpleadapter

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

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

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) 

下次启动应用程序时,确定。 所以每次启动都会发生错误。

2 个答案:

答案 0 :(得分:2)

您在SimpleAdapter中创建了onResume,但此处发生的是您已在onCreate中设置了textChangedListener。调用onCreate后,Android会恢复视图状态,包括视图中的文本。这会在onResume运行之前触发文本更改侦听器并创建SimpleAdapter。我认为您可以将SimpleAdapter设置移至onCreate,然后它就可以正常工作。

答案 1 :(得分:2)

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