Unable To Load Fragment From Activity
I am using Sherlock Fragments library to sliding menu.my problem is i unable to load fragment when i click button in activity. I am getting java.lang.classcastException to android.
Solution 1:
Fragment2
is a fragment not a Activity. Fragment is hosted by a Activity. The below is wrong
Intent action=newIntent(v.getContext().getApplicationContext(),Fragment2.class);
startActivity(action);
You are missing setContentView
for the Activity
.
publicclassAmecmainActivityextendsFragmentActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // missing
Then
You need a Container in activity_main.xml
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?android:attr/detailsElementBackground" />
Then in onClick
FragmentManagerfragmentManager= getSupportFragmentManager();
FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
Fragment2fragment=newFragment2();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Edit:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ImageButtonandroid:id="@+id/imageButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:src="@drawable/ic_launcher" /><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/imageButton1"android:id="@+id/fragment_container"
></FrameLayout></RelativeLayout>
Solution 2:
You can't call Fragment Like this:
You have to use this:
voidaddfragment(Fragment fragment, boolean addBacktoStack, int transition) {
FragmentTransactionft= getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, fragment);
ft.setTransition(transition);
if (addBacktoStack)
ft.addToBackStack(null);
ft.commit();
}
And call this method like this:
addfragment(newFragment2(contextHere),true,FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Solution 3:
In the activity, the fragment can be loaded using;
FragmentManagerfragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container_main, newMyFragment())
.commit();
where MyFragment is as;
publicclassMyFragmentextendsandroid.support.v4.app.Fragment {
publicMyFragment() {
// Required empty public constructor
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewrootView= inflater.inflate(R.layout.fragment_whats_trending, container, false);
return rootView;
}
}
and a simple Fragment xml, would look like:
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="in.stocksphere.stocksphere.MyFragment$PlaceholderFragment" >
<TextView android:text="@string/ta_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Post a Comment for "Unable To Load Fragment From Activity"