2013-10-20 52 views
0

我寫了一個android應用程序,它在特定情況下崩潰。然而,因爲我對android不是很有經驗,所以我不清楚我必須做什麼(儘管棧跟蹤看起來非常清晰)。我的應用程序只在描述的情況下崩潰:java.lang.IllegalStateException後臺線程更新列表視圖

所以這就是發生了什麼:我的應用程序正在運行,手機被喚醒。大約5分鐘後,通過觸摸設備不動作,手機進入睡眠狀態並關閉屏幕。當我現在嘗試再次喚醒手機時,手機會再次顯示我的應用程序屏幕。但是,在2秒左右之後,我收到了我的應用程序崩潰並需要系統停止的信息。

原因是,一個列表視圖適配器(所以我認爲)嘗試更新列表視圖,但不是從主要的UI線程內。這裏是堆棧跟蹤:

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) 

特別

java.lang.IllegalStateException:適配器的內容發生了變化,但ListView控件沒有收到通知。確保適配器的內容不會從後臺線程修改,而只能從UI線程修改。 [在ListView(2131230724,類eu.erikw.PullToRefreshListView)與適配器(類android.widget.HeaderViewListAdapter)]

儘管錯誤消息是相當詳細的,我仍然不知道我需要做什麼?誰能幫我?

編輯:eu.erikw.PullToRefreshListView的代碼是要長的張貼在這裏在stackoverflow。但你從這裏得到的代碼(這是一個sinlge文件):https://github.com/erikwt/PullToRefresh-ListView/blob/master/libraryproject/src/eu/erikw/PullToRefreshListView.java

編輯2:我知道現在它崩潰的地方。看看下面的代碼

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 
    } 

正如你可以看到,它崩潰,雖然我檢查,如果我在主要的UI線程。因爲這個類擴展了一個listview,所以它不能實現可運行的方法。所以,由於你的建議不適用,我還能做什麼?

+0

當您修改適配器的內容而不調用'notifyDataSetChanged()'時,會發生這種情況。 – GVillani82

回答

1

如果您調用notifyDataSetChanged或任何與背景相關的視圖,就會發生這種情況。從eu.erikw.PullToRefreshListView發佈代碼。

此外,要在ui線程上運行,只需調用。

activity.runOnUIThread(new Runnable(){ 

// runnable code here. 

}); 

像setText/setImage這樣的動作也必須在uiThread上運行,你能指出哪種方法失敗嗎?

+0

我在我的項目中搜索notifyDataSetChanged,並在eu.erikw.PullToRefreshListView中搜索。我發現它的唯一地方是在我的代碼中,我檢查了這部分代碼在應用程序崩潰之前未被執行。所以這對我來說可能不是問題。我認爲它一定是在這個pull2refresh代碼 – toom

+0

張貼代碼的地方然後,所以我們可以告訴你:) –

+0

該代碼是我添加到我的問題的鏈接後面。 – toom