Reputation: 292
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.app.activity.
I have imported:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
but still I am getting error.
code for mainActivity.
public class AmecmainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionnetwork=(ImageButton)findViewById(R.id.ActionNetwork);
actionnetwork.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent action=new Intent(v.getContext().getApplicationContext(),
Fragment2.class);
startActivity(action);
}
});
and code in fragment2
public class Fragment2 extends Fragment{
Fragment fragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.actionnetworklogin, container, false);
Button login = (Button)view.findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
}
return view;
can any one help me
framelayout xml
<?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" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
onclick method :
actionnetwork=(ImageButton)findViewById(R.id.ActionNetwork);
actionnetwork.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new Fragment2();
FragmentManager fm =getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
}
});
fragment2 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Second Fragment"
android:textSize="15pt" />
</RelativeLayout>
mainactivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:scaleType="fitXY" >
<ImageButton
android:id="@+id/ActionNetwork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="25dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
code for pervious fragment
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuIcon:
toggle();
break;
case android.R.id.home:
getFragmentManager().popBackStackImmediate();
}
return super.onOptionsItemSelected(item);
}
fragment2.class file
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
Button clicked=(Button)view.findViewById(R.id.clickedmove);
clicked.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = new Fragment2();
FragmentManager fm =getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.contentFragment, fragment);
transaction.commit();
}
});
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Upvotes: 0
Views: 10871
Reputation: 4152
In the activity, the fragment can be loaded using;
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container_main, new MyFragment())
.commit();
where MyFragment is as;
public class MyFragment extends android.support.v4.app.Fragment {
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = 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>
Upvotes: 1
Reputation: 133560
Fragment2
is a fragment not a Activity. Fragment is hosted by a Activity. The below is wrong
Intent action=new Intent(v.getContext().getApplicationContext(),Fragment2.class);
startActivity(action);
You are missing setContentView
for the Activity
.
public class AmecmainActivity extends FragmentActivity {
@Override
protected void onCreate(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
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment = new Fragment2();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Edit:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android: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" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/imageButton1"
android:id="@+id/fragment_container"
>
</FrameLayout>
</RelativeLayout>
Upvotes: 3
Reputation: 18933
You can't call Fragment Like this:
You have to use this:
void addfragment(Fragment fragment, boolean addBacktoStack, int transition) {
FragmentTransaction ft = 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(new Fragment2(contextHere),true,FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Upvotes: 1