Why The Fragment Doesn't Work
Please help. What happens? What is the cause of error, and how could I solve it? Thanks for your help! > java.lang.RuntimeException: Unable to start activity > ComponentIn
Solution 1:
(Always try to use the support libraries as you are doing) First: 1. Should use a FragmentActivity and NO Activity 2. Should use a Fragment to the child components you'll be using. 3. On the Fragments on the onCreateView just do the Inflate 4. On the Fragment use the onViewCreated to find the TextView. Sample: //This is the Activity
publicclassMainActivityextendsFragmentActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//This is the layout.activity_main
<RelativeLayoutxmlns: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" ><fragmentandroid:name="com.example.fragmentapp.ChildFragment"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/textView1"android:layout_alignParentTop="true" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:padding="@dimen/padding_medium"android:text="@string/hello_world"tools:context=".MainActivity" /></RelativeLayout>
//This is the Fragment to be shown
publicclassChildFragmentextendsFragment {
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.child_frag, null);
}
@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextViewtvText= (TextView)view.findViewById(R.id.textView1);
tvText.setText("Found!");
}
}
//This is the layout for the fragment layout.child_fragment
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout>
Post a Comment for "Why The Fragment Doesn't Work"