替换片段和重新调整活动将崩溃

时间:2016-02-22 03:04:54

标签: android android-fragments

我的功能是单击FagmentOne ImageView,它将自己替换为FragmentTwo,

并单击FragmentTwo textView将重新调整Activity。

但是重新调整活动会崩溃。

为什么以及如何解决它??

这是活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.testfunction.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/fragmentBlock"
        android:background="@android:color/holo_blue_light"/>

    <RelativeLayout
        android:id="@+id/fragmentBlock"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <fragment
            android:tag="fra"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.testfunction.FragmentOne"/>
    </RelativeLayout>

</RelativeLayout>

FragmentOne布局:

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

    <ImageButton
        android:id="@+id/imageButton_switch"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:src="@android:drawable/btn_radio"
        android:onClick="go" />

</LinearLayout>

FragmentTwo Layout:

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

    <TextView
        android:id="@+id/textView_status"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Large Text" />

</LinearLayout>

活动代码:

private FragmentManager mManager;
    private Fragment two = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mManager = getFragmentManager();
    }

    public void go(View view){
        FragmentTransaction ft = mManager.beginTransaction();
        if(two == null){
            two = new FragmentTwo();
        }
        ft.replace(R.id.fragmentBlock, two,"fra");
        ft.commit();
    }

FragmentTwo代码:

LinearLayout root = null;
    TextView status = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return root = (LinearLayout) inflater.inflate(R.layout.fragment_bar_status, container,false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(status == null){
            status = (TextView) root.findViewById(R.id.textView_status);
            status.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        getActivity().recreate();
    }

错误消息:

Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
    at android.app.Activity.setContentView(Activity.java:2166)
    at com.example.testfunction.MainActivity.onCreate(MainActivity.java:19)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    ... 10 more
 Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    ... 18 more
 Caused by: java.lang.IllegalStateException: Fragment com.example.testfunction.FragmentOne did not create a view.

感谢。

添加

FragmentOne代码:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_bar_layout, container,false);
    }

}

0 个答案:

没有答案