Java 从一个片段移动到另一个片段时出错
错误显示无法将突出显示强制转换为片段,无法将概述强制转换为片段,错误仅出现在selectFrag方法中。请任何人帮我解决这个问题。我想在单击textview项时将一个片段切换到另一个片段Java 从一个片段移动到另一个片段时出错,java,android,Java,Android,错误显示无法将突出显示强制转换为片段,无法将概述强制转换为片段,错误仅出现在selectFrag方法中。请任何人帮我解决这个问题。我想在单击textview项时将一个片段切换到另一个片段 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved
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>
您将fr声明为片段,但创建了一个突出显示 突出显示扩展了片段,但您需要将其强制转换为片段,以便在FragmentTransaction中使用它 所以试试这个:
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();
}