【发布时间】:2013-12-27 09:45:39
【问题描述】:
我正在使用 Sherlock Fragments 库来滑动菜单。我的问题是当我在活动中单击按钮时无法加载片段。我得到 java.lang.classcastException 到 android.app.activity。
我已经导入了:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
但我仍然遇到错误。
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);
}
});
片段2中的代码
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;
谁能帮帮我
框架布局 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>
点击方法:
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>
上一个片段的代码
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 文件
公共类 Fragment1 扩展 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);
}
}
【问题讨论】:
-
您是否尝试过扩展 SherlockFragment 和 SherlockFragmentActivity 而不是 Fragment 和 FragmentActivity?
-
不,我没有延长
-
所以试试吧,但我不确定这会有所帮助:)
-
@user3069112 发布堆栈跟踪
-
@user3069112 片段不是活动。你需要一个容器,并且需要将片段添加到容器中