在我的应用程序中,我启动一个活动,然后从那里启动另一个活动。在第二个活动中,我展示了一个填充了数据的 GridView。然后,用户可以按一个按钮来删除附加到 GridView 的所有数据。每当用户删除数据然后按下返回按钮时,应用程序就会崩溃并出现致命异常。在下面的代码中,logcat 表示崩溃发生在 ReloadGridView 方法中。这段代码中实际上有 try...catch ,但这并不能防止致命错误。
我花了一天多的时间试图找出导致这种情况的原因,但没有成功。仅当附加到适配器的基础数据被删除并按下返回按钮时才会发生。此代码在 Android 4.1 上运行。注意:这是一组精简的代码。所有必要的东西都被去掉了,以便更容易集中精力解决问题。
@Override
public void onCreate(Bundle savedInstanceState)
{
private ArrayList data = new ArrayList();
private MyAdapter adapterData;
private GridView gvData;
// Setup the GridView.
gvData = (GridView) findViewById(R.id.gvData);
adapterData = new MonitorDataAdapter();
gvData.setAdapter(adapterData);
new Thread(null, new LoadMyDataRunnable(0, false), "LoadMyDataRunnable").start();
}
private class LoadMyDataRunnable implements Runnable
{
@Override
public void run()
{
data = GetMyData();
ReloadGridView reloadGridView = new ReloadGridView();
runOnUiThread(reloadGridView);
}
}
private class ReloadGridView implements Runnable
{
@Override
public void run()
{
// Tried the following and many other things but a fatal exeption occurs here when the Back button is pressed:
// adapterData = new MyAdapter();
// gvData.setAdapter(adapterData);
// if (data.size() == 0)
// gvData.setAdapter(null);
}
}
// Button to delete data.
btnDelete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Delete the data.
SomeMethodToDeleteData();
new Thread(null, new LoadMyDataRunnable(0, false), "LoadMyDataRunnable").start();
}
}
日志:
07-08 11:28:15.421: E/AndroidRuntime(27415): FATAL EXCEPTION: MonitorDevice.ReloadGridView.run_d99af1dd-b961-4a1f-bea5-4e4bb986e107
07-08 11:28:15.421: E/AndroidRuntime(27415): java.lang.NullPointerException
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.AbsListView.obtainView(AbsListView.java:2269)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.GridView.onMeasure(GridView.java:1030)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-08 11:28:15.421: E/AndroidRuntime(27415): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2165)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.View.measure(View.java:15222)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1870)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1106)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1279)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4238)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.os.Handler.handleCallback(Handler.java:615)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.os.Looper.loop(Looper.java:137)
07-08 11:28:15.421: E/AndroidRuntime(27415): at android.app.ActivityThread.main(ActivityThread.java:4838)
07-08 11:28:15.421: E/AndroidRuntime(27415): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:28:15.421: E/AndroidRuntime(27415): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 11:28:15.421: E/AndroidRuntime(27415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-08 11:28:15.421: E/AndroidRuntime(27415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
07-08 11:28:15.421: E/AndroidRuntime(27415): at dalvik.system.NativeStart.main(Native Method)
07-08 11:28:15.971: E/WifiHW(427): wifi_send_command : SIGNAL_POLL ; interface index=0;
07-08 11:28:18.981: E/WifiHW(427): wifi_send_command : SIGNAL_POLL ; interface index=0;
07-08 11:28:22.001: E/WifiHW(427): wifi_send_command : SIGNAL_POLL ; interface index=0;
07-08 11:28:25.011: E/WifiHW(427): wifi_send_command : SIGNAL_POLL ; interface index=0;
07-08 11:28:28.031: E/WifiHW(427): wifi_send_command : SIGNAL_POLL ; interface index=0;