0

我的应用程序在最初运行时可以工作,但是如果它在后台停留一段时间然后由用户重新启动,我会收到 RuntimeException“无法启动活动 ComponentInfo...错误膨胀类片段”。

该活动包含一个包含 ListView 的片段。ListView 有一个自定义适配器,其中包含一个 ImageView 和两个 TextView。

在应用程序处于后台并使用许多其他应用程序后返回活动时,会引发错误,这使我相信这是内存问题,因为仅在我的设备上的模拟器不会发生这种情况。

如何使我的应用程序避免此异常?

这是我在后台很长时间后恢复应用程序后的 LogCat:

06-15 18:48:41.886: E/AndroidRuntime(10033): FATAL EXCEPTION: main
06-15 18:48:41.886: E/AndroidRuntime(10033): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scl.tntmg/com.scl.tntmg.NowShowingActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.os.Looper.loop(Looper.java:137)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at java.lang.reflect.Method.invoke(Method.java:511)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at dalvik.system.NativeStart.main(Native Method)
06-15 18:48:41.886: E/AndroidRuntime(10033): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.Activity.setContentView(Activity.java:1835)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.scl.tntmg.NowShowingActivity.onCreate(NowShowingActivity.java:16)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.Activity.performCreate(Activity.java:4465)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-15 18:48:41.886: E/AndroidRuntime(10033):    ... 11 more
06-15 18:48:41.886: E/AndroidRuntime(10033): Caused by: android.os.NetworkOnMainThreadException
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.scl.tntmg.util.MovieGuideUtil.getData(MovieGuideUtil.java:118)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.scl.tntmg.util.MovieGuideUtil.refreshMovies(MovieGuideUtil.java:183)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.scl.tntmg.util.MovieGuideUtil.getMovies(MovieGuideUtil.java:82)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at com.scl.tntmg.ListFrag.onCreateView(ListFrag.java:49)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:289)
06-15 18:48:41.886: E/AndroidRuntime(10033):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
06-15 18:48:41.886: E/AndroidRuntime(10033):    ... 21 more

这是活动:

public class NowShowingActivity extends FragmentActivity{

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nowshowing);

    removeNotifications();
}

public void onRestart(){
    super.onRestart();
}

public void onStop(){
    super.onStop();
}

public void onDestroy(){
    super.onDestroy();
}

public void onResume(){
    super.onResume();
    removeNotifications();
}

private void removeNotifications(){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    mNotificationManager.cancel(1);
}

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mgoptions,menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    Intent myIntent;
    switch(item.getItemId()){
        case R.id.theater_details:
            myIntent = new Intent(this, TheaterActivity.class);
            this.startActivity(myIntent);
            return true;
        case R.id.refresh:
            myIntent = new Intent(this, HomeActivity.class);
            this.startActivity(myIntent);
            return true;
        case R.id.about:
            myIntent = new Intent(this, AboutActivity.class);
            this.startActivity(myIntent);
            return true;
        case R.id.settings:
            myIntent = new Intent(this, SettingActivity.class);
            this.startActivity(myIntent);
            return true;
        default:
            return super.onOptionsItemSelected(item);   
    }
}

这是现在显示的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >



<fragment class="com.scl.tntmg.ListFrag"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp" android:layout_height="fill_parent" >
    <!-- Preview: layout=@layout/list -->
</fragment>

</LinearLayout>
4

2 回答 2

2

你的堆栈跟踪说

Caused by: android.os.NetworkOnMainThreadException

这意味着也许第一次,您在正确的线程中访问网络,但在第二次恢复后,网络是从 UI 线程访问的,如下所示:

at com.scl.tntmg.util.MovieGuideUtil.getData(MovieGuideUtil.java:118)
于 2012-06-16T00:07:12.063 回答
0

正如您所说,这将是一个内存问题,当您的应用程序失去焦点时,android 可能会或可能不会(取决于资源的可用性)释放您的应用程序使用的内存。因此,您应该将活动的状态保存在 onPause() 函数中,然后可以在 onResume() 函数中恢复它。

于 2012-06-15T23:22:29.353 回答