0

I've written an android app and it crashes in a certain situation. However since I'm not very experienced with android it is not clear to me what I have to do (although the stacktrace seems to be very clear). My app only crashes in the described situation:

So this is what happens: My app is running and the phone is awaken. After 5 minutes or so after no action by touching the device the phone goes to sleep and turns off the screen. When I now try to wake up the phone again the phone shows my app screen again. However, after 2 seconds or so I get the information that my app crashed and need to be stopped by the system.

The reason for that is that a listview adapter (so I think) tries to update the listview but not from within the main ui thread. Here is the stacktrace:

10-20 23:32:04.290: W/System.err(8276): java.net.UnknownHostException: www.example.de
10-20 23:32:04.330: W/System.err(8276): java.net.UnknownHostException: www.example.de
10-20 23:32:04.330: W/System.err(8276):     at java.net.InetAddress.lookupHostByName(InetAddress.java:499)
10-20 23:32:04.330: W/System.err(8276):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:296)
10-20 23:32:04.330: W/System.err(8276):     at java.net.InetAddress.getAllByName(InetAddress.java:258)
10-20 23:32:04.330: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
10-20 23:32:04.330: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-20 23:32:04.330: W/System.err(8276):     at java.net.InetAddress.lookupHostByName(InetAddress.java:499)
10-20 23:32:04.330: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
10-20 23:32:04.330: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-20 23:32:04.330: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
10-20 23:32:04.330: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
10-20 23:32:04.340: W/System.err(8276):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:296)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:406)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:387)
10-20 23:32:04.350: W/System.err(8276):     at java.net.InetAddress.getAllByName(InetAddress.java:258)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1021)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
10-20 23:32:04.350: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258)
10-20 23:32:04.350: W/System.err(8276):     at de.example.logic.DataLoader.getJSONFromUrl(DataLoader.java:89)
10-20 23:32:04.350: W/System.err(8276):     at de.example.logic.DataLoader.doInBackground(DataLoader.java:68)
10-20 23:32:04.350: W/System.err(8276):     at de.example.logic.DataLoader.doInBackground(DataLoader.java:1)
10-20 23:32:04.350: W/System.err(8276):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-20 23:32:04.360: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
10-20 23:32:04.360: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
10-20 23:32:04.360: W/System.err(8276):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-20 23:32:04.360: W/System.err(8276):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-20 23:32:04.360: W/System.err(8276):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-20 23:32:04.360: W/System.err(8276):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
10-20 23:32:04.360: W/System.err(8276):     at de.example.logic.RemoteLogging.doInBackground(RemoteLogging.java:119)
10-20 23:32:04.360: W/System.err(8276):     at de.example.logic.RemoteLogging.doInBackground(RemoteLogging.java:1)
10-20 23:32:04.360: W/System.err(8276):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-20 23:32:04.360: W/System.err(8276):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-20 23:32:04.360: W/System.err(8276):     at java.lang.Thread.run(Thread.java:1019)
10-20 23:32:04.370: W/System.err(8276):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-20 23:32:04.370: W/System.err(8276): java.lang.NullPointerException
10-20 23:32:04.370: W/System.err(8276):     at de.example.logic.JSONData.createObjectsFromJSON(JSONData.java:57)
10-20 23:32:04.370: W/System.err(8276):     at de.example.logic.JSONData.updateViews(JSONData.java:34)
10-20 23:32:04.370: W/System.err(8276):     at de.example.logic.DataLoader.onPostExecute(DataLoader.java:108)
10-20 23:32:04.370: W/System.err(8276):     at de.example.logic.DataLoader.onPostExecute(DataLoader.java:1)
10-20 23:32:04.370: W/System.err(8276):     at android.os.AsyncTask.finish(AsyncTask.java:417)
10-20 23:32:04.370: W/System.err(8276):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-20 23:32:04.370: W/System.err(8276):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-20 23:32:04.370: W/System.err(8276):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 23:32:04.370: W/System.err(8276):     at android.os.Looper.loop(Looper.java:130)
10-20 23:32:04.370: W/System.err(8276):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-20 23:32:04.370: W/System.err(8276):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 23:32:04.370: W/System.err(8276):     at java.lang.reflect.Method.invoke(Method.java:507)
10-20 23:32:04.370: W/System.err(8276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
10-20 23:32:04.370: W/System.err(8276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
10-20 23:32:04.370: W/System.err(8276):     at dalvik.system.NativeStart.main(Native Method)
10-20 23:32:04.370: W/System.err(8276):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-20 23:32:04.370: W/System.err(8276):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-20 23:32:04.370: W/System.err(8276):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-20 23:32:04.380: W/System.err(8276):     at java.lang.Thread.run(Thread.java:1019)
10-20 23:32:06.350: W/dalvikvm(8276): threadid=1: thread exiting with uncaught exception (group=0x401e9560)
10-20 23:32:06.350: E/AndroidRuntime(8276): FATAL EXCEPTION: main
10-20 23:32:06.350: E/AndroidRuntime(8276): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230724, class eu.erikw.PullToRefreshListView) with Adapter(class android.widget.HeaderViewListAdapter)]
10-20 23:32:06.350: E/AndroidRuntime(8276):     at android.widget.ListView.layoutChildren(ListView.java:1510)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at android.widget.AbsListView$CheckForTap.run(AbsListView.java:2005)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at android.os.Handler.handleCallback(Handler.java:587)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at android.os.Looper.loop(Looper.java:130)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at java.lang.reflect.Method.invoke(Method.java:507)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
10-20 23:32:06.350: E/AndroidRuntime(8276):     at dalvik.system.NativeStart.main(Native Method)

Particularly

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230724, class eu.erikw.PullToRefreshListView) with Adapter(class android.widget.HeaderViewListAdapter)]

Although the error message is quite verbose I still do not know what I need to do? Can anyone help me?

EDIT: The code for eu.erikw.PullToRefreshListView is to long for posting it here on stackoverflow. But you get the code from here (it is a sinlge file): https://github.com/erikwt/PullToRefresh-ListView/blob/master/libraryproject/src/eu/erikw/PullToRefreshListView.java

EDIT2: I know now where it crashes. Have a look at the following code

public class PullToRefreshListView extends ListView{
    ...other code...


    // It crashes in this method
    @Override
    public boolean onTouchEvent(MotionEvent event){
        Log.d("pull2refresh","onTouchEvent"); 
        // it also crashes although I make this check
        if( !(  Looper.getMainLooper().equals(Looper.myLooper()) ) ) 
                return true;


        if(lockScrollWhileRefreshing
            && (state == State.REFRESHING || getAnimation() != null &&     !getAnimation().hasEnded())){
            return true;
        ... other code in this method...
        return super.onTouchEvent(event); <-- it crashes exactly here at the end of the whole method
    }

As you can see, it crashes although I check if I'm in the main ui thread. Additinally since this class extends a listview it cannot implement the runable method. So since your suggestions are not applicable what else could I do?

toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

1

This happens, if you call notifyDataSetChanged or anything view related from the background. post your code from eu.erikw.PullToRefreshListView.

Besides that, to run on ui thread simply call.

activity.runOnUIThread(new Runnable(){

// runnable code here.

});

actions like setText/setImage also has to be run on uiThread, can you indicate which method fails?

Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
  • I search for notifyDataSetChanged in my project and also in eu.erikw.PullToRefreshListView. The only place I found it was in my code and I checked that this part of the code is not executed before the app crashes. So this probably is not the problem for me. I think it must be somewhere in this pull2refresh code – toom Oct 21 '13 at 16:32