中將一個片段切換到另一個片段錯誤顯示無法將高光突出顯示爲片段,並且無法將片段概覽轉換爲片段,並且僅在selectFrag()方法中出現錯誤。請任何人幫我解決這個問題。當我點擊textview項目時,我想切換一個片段到其他片段。無法在android
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>
請貼'你的類import's。 – 2014-11-20 18:30:59