2017-02-02 1 views
0

Ich habe einen SimpleAdapter mit Filterung inputSearch und anklickbare Elemente (die ich von MySQL-Datenbank erhalten).SimpleAdapter stürzt jede zweite Startzeit

Manchmal, wenn ich das Mittagessen mein Projekt ein Fehler ist:

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)  

Wenn ich die Anwendung nächste Mal starten, ist es ok. Der Fehler tritt also bei jedem zweiten Start auf.

+1

können Sie den Code Ihres ChooseGroupActivity eingeben? – Alvaro

+0

Ich habe den Code hinzugefügt. –

Antwort

2

Entfernen Sie die Initialisierung von simpleAdapter von onResume. In Ihrem 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); 
     } 
+0

Vielen Dank! Funktioniert. :) –

2

Sie erstellen die SimpleAdapter in onResume, aber was passiert hier ist, dass Sie Ihre textChangedListener in onCreate festgelegt haben. Nachdem onCreate aufgerufen wurde, stellt Android den Ansichtszustand einschließlich des Textes in Ihrer Ansicht wieder her. Dies löst den Textwechsel-Listener aus, bevor onResume ausgeführt wird, und erstellt den . Ich denke, dass Sie in Ordnung sein werden, um Ihre SimpleAdapter Einstellung auf onCreate zu verschieben, und dann sollte es einfach funktionieren.

+0

Danke. :) Funktioniert! –