错误显示无法强制转换为片段并且无法将概述转换为片段,并且仅在selectFrag()方法中出现错误。请任何人帮我解决这个问题。当我点击textview项目时,我想将一个片段切换到另一个片段。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view){
Fragment fr;
if (view == findViewById(R.id.txt_highlite)) {
fr = (Fragment) new Highlight();
}
else{
fr = (Fragment) new Overview();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.place_holder, fr);
fragmentTransaction.commit();
}
}
//突出显示课程
public class Highlight extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.highlight, container, false);
}
}
//概述课
public class Overview extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.overview, container, false);
}
}
// activity_main.xml中
<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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp" >
<LinearLayout
android:id="@+id/lin_projectoverview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp" >
<TextView
android:id="@+id/text_Projectoverview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Project Overview"
android:textSize="26sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/project_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="65dp"
android:gravity="center" >
<TextView
android:id="@+id/txt_overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="12dp"
android:text="Overview" />
<TextView
android:id="@+id/txt_highlite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="12dp"
android:text="Highlight" />
</LinearLayout>
</RelativeLayout>
<fragment
android:name="com.example.fragment_move.Overview"
android:id="@+id/place_holder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
答案 0 :(得分:0)
检查您的import语句。根据您的getFragmentManager()
,您的每个扩展Fragment
的类都应该只导入此片段:
import android.app.Fragment;
答案 1 :(得分:0)
突出显示和概述已经从Fragment继承,那你为什么要再次将它们转换为片段呢? 我建议你这样做。
public void selectFrag(View view){
Fragment fr;
if (view.getId() == R.id.txt_highlite) {
fr = new Highlight();
}
else{
fr = new Overview();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.place_holder, fr);
fragmentTransaction.commit();
}
并确保您的Fragment导入始终来自支持库或其他