我写了一个Android应用程序,它在某种情况下崩溃了。然而,由于我对android不是很有经验,所以我不清楚我必须做什么(虽然堆栈跟踪似乎非常清楚)。我的应用程序仅在描述的情况下崩溃:
所以这就是:我的应用程序正在运行,手机被唤醒了。通过触摸设备在没有动作5分钟左右后,手机进入睡眠状态并关闭屏幕。当我现在尝试再次唤醒手机时,手机会再次显示我的应用屏幕。但是,在2秒左右之后,我得到了我的应用程序崩溃并需要被系统停止的信息。
原因是listview适配器(我认为)尝试更新listview但不是来自主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)中使用Adapter(类android.widget.HeaderViewListAdapter)]
虽然错误信息非常冗长但我仍然不知道我需要做什么?任何人都可以帮助我吗?
编辑:eu.erikw.PullToRefreshListView的代码很长,可以在stackoverflow上发布。但是你从这里得到代码(这是一个sinlge文件):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线程中,但它崩溃了。另外,由于此类扩展了listview,因此无法实现可运行的方法。所以既然你的建议不适用我还能做什么呢?
答案 0 :(得分:1)
如果您调用notifyDataSetChanged或从后台相关的任何视图,则会发生这种情况。从eu.erikw.PullToRefreshListView发布您的代码。
除此之外,要在ui线程上运行,只需调用。
activity.runOnUIThread(new Runnable(){
// runnable code here.
});
像setText / setImage这样的动作也必须在uiThread上运行,你能指出哪个方法失败了吗?