当屏幕方向改变IllegalStateException异常没有创建一个视图 [英] illegalstateexception did not create a view when screen orientation changes

查看:219
本文介绍了当屏幕方向改变IllegalStateException异常没有创建一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图使一项活动(GradeListActivity)打开一个ListView片段(GradeListFragment)的应用程序。当我点击一个项目,它会打开一个新的片段(GradeDetailFragment)。如果在纵向模式下,它取代了片段1列表片段。如果在景观,它取代了空fragment2。我的问题是,当我切换方向的应用程序崩溃。从两个角度切换得到了同样的错误。

I have been trying to make an app that opens a listview fragment (GradeListFragment) from an activity (GradeListActivity). When I click an item, it opens a new fragment (GradeDetailFragment). If in portrait mode, it replaces the list fragment in fragment1. If in landscape, it replaces the empty fragment2. My problem is that the app crashes when I switch orientation. Switching from both perspectives gets the same error.

04-09 12:10:52.240: E/AndroidRuntime(2221): FATAL EXCEPTION: main
04-09 12:10:52.240: E/AndroidRuntime(2221): Process:     bcs421.christophergoepfert.hwk.gradeapp.presentation, PID: 2221
04-09 12:10:52.240: E/AndroidRuntime(2221): java.lang.RuntimeException:   Unable to start activity    ComponentInfo{bcs421.christophergoepfert.hwk.gradeapp.presentation/bcs421.christ     ophergoepfert.hwk.gradeapp.presentation.GradeListActivity}:   android.view.InflateException: Binary XML file line #8: Error inflating class   fragment
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread.access$900(ActivityThread.java:144)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at    android.os.Handler.dispatchMessage(Handler.java:102)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.os.Looper.loop(Looper.java:135)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread.main(ActivityThread.java:5221)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at java.lang.reflect.Method.invoke(Native Method)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at java.lang.reflect.Method.invoke(Method.java:372)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-09 12:10:52.240: E/AndroidRuntime(2221): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.Activity.setContentView(Activity.java:2144)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at bcs421.christophergoepfert.hwk.gradeapp.presentation.GradeListActivity.onCreate(GradeListActivity.java:29)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.Activity.performCreate(Activity.java:5933)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-09 12:10:52.240: E/AndroidRuntime(2221):     ... 11 more
04-09 12:10:52.240: E/AndroidRuntime(2221): Caused by: java.lang.IllegalStateException: Fragment android.app.ListFragment did not create a view.
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.app.Activity.onCreateView(Activity.java:5282)
04-09 12:10:52.240: E/AndroidRuntime(2221):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)

在重新启动时MainActivity不会崩溃。

MainActivity does not crash when it restarts.

public class MainActivity extends Activity{

public GradeCollection collection;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        //grade data 
    }

}
public void startGradeListActivity(View v){

    Intent i = new Intent(MainActivity.this, GradeListActivity.class);
    //putextra gradecollection
    startActivity(i);

    }
}

该main_activity XML

The main_activity xml

<TextView
    android:id="@+id/textViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/name" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="4"
    android:stepSize="1" />

<TextView
    android:id="@+id/textViewGradeNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/gradeNumber" />

<TextView
    android:id="@+id/textViewGradeLetter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/buttonViewGrades"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="startGradeListActivity"
    android:text="@string/viewGrades" />

GradeListActivity崩溃在ft.commit();也许问题在于我进口android.support.v4.app.FragmentActivity;

GradeListActivity crashes at ft.commit();, maybe the problem is that I'm importing android.support.v4.app.FragmentActivity;?

public class GradeListActivity extends FragmentActivity {
//private GradeCollection listGrades;
//private ArrayList<String> listCatagory;
//private ArrayAdapter<String> listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.catagory_list);

    ListFragment fragment = new GradeListFragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment1, fragment);

    ft.commit();

    }
}

这是GradeListFragment。

This is GradeListFragment.

public class GradeListFragment extends ListFragment{

 @Override
 public View onCreateView(  LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_category, container, false);
       //data handling
        return v;
     }
 }

下面是XML的catagory_list纵向

Here is the xml for catagory_list portrait

<fragment
    android:id="@+id/fragment1"
    android:name="android.app.ListFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.03" />

和这里的山水

<fragment
    android:id="@+id/fragment1"
    android:name="android.app.ListFragment"
    android:layout_width="271dp"
    android:layout_height="match_parent" />

<FrameLayout
    android:id="@+id/fragment2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我省略传递的实际数据以及GradeDetailFragment,因为我知道这些都不是问题。我见过<一个href=\"http://stackoverflow.com/questions/7707032/illegalstateexception-when-replacing-a-fragment\">this页面,但我真的不明白它是如何工作的,所以我不能测试它。我曾尝试onConfigChange在清单中,但我就不能更改为相应的布局。如果有人能解决这个问题或解释的链接,我将非常感激。

I have omitted the actual data being passed as well as GradeDetailFragment because I know those aren't the problem. I have seen this page but I do not really understand how it works so I can't test it. I have tried onConfigChange in the manifest but then I can't change to the appropriate layout. If anyone could solve this issue or explain the link, I would be very grateful.

推荐答案

看这个文档:

请注意:当你通过定义布局XML文件中的片段添加一个片段一个活动的布局,则无法在运行时删除片段。如果您打算交换你的片段,并进行用户交互过程中,必须将片段添加到该活动第一次启动活动时。

Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts.

所以你不能替换 R.id.fragment1 在运行时一样:

So you can not replace the R.id.fragment1 at runtime like:

ft.replace(R.id.fragment1, fragment); //wrong

然后执行片段在运行时,您可以使用片段容器的FrameLayout 来代替改变了正确的方法片段的你的 R.id.fragment1 为:

then the correct way to implement fragment changing at runtime, you can use fragment container FrameLayout to replace Fragment for your R.id.fragment1 as:

<FrameLayout
   android:id="@+id/fragment1"       
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1.03" />

那么你可以添加/ R.id.fragment1

希望这有助于!

这篇关于当屏幕方向改变IllegalStateException异常没有创建一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆