我编写了一个 android 应用程序,它在某种情况下崩溃了。但是,由于我对 android 不是很有经验,所以我不清楚我必须做什么(尽管堆栈跟踪似乎很清楚)。我的应用程序仅在所描述的情况下崩溃:
所以这就是发生的事情:我的应用程序正在运行并且电话被唤醒。触摸设备后 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, class eu.erikw.PullToRefreshListView) 和 Adapter(class android.widget.HeaderViewListAdapter)]
尽管错误消息非常冗长,但我仍然不知道我需要做什么?谁能帮我?
编辑: eu.erikw.PullToRefreshListView 的代码很长,因为它需要在 stackoverflow 上发布。但是你从这里得到代码(它是一个单一的文件):https ://github.com/erikwt/PullToRefresh-ListView/blob/master/libraryproject/src/eu/erikw/PullToRefreshListView.java
EDIT2:我现在知道它在哪里崩溃了。看看下面的代码
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 线程中,但它还是崩溃了。另外,由于此类扩展了列表视图,因此它无法实现可运行方法。因此,由于您的建议不适用,我还能做什么?