My function is click FagmentOne ImageView,it will replace itself to FragmentTwo,
and click FragmentTwo textView will recrate Activity.
but recrate Activity will crash.
Why and how to fix it...?
this is Activity Layout:
<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 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" >
<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>
Activity Code:
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 code:
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();
}
error message:
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.
Thanks.
Add
FragmentOne code:
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);
}
}