3
我試圖根據GPS是否啓用顯示不同的片段。但我收到此錯誤: -更改片段
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0e00a1
請告訴我如何做到這一點:
這裏是我的MainActivity類別:
public class MainActivity extends Activity {
Fragment fr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
fr = new FragmentTwo();
getFragment();
} else {
fr = new FragmentOne();
getFragment();
}
}
public void getFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne: -
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
FragmentTwo: -
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
我activity_main: -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
而且fragment_one.xml和fragment_two.xml是相同的只是不同的文本: -
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="GPS IS ENABLE"/>
</RelativeLayout>
在此先感謝。
但現在它給了java.lang.RuntimeException:無法啓動活動@Dhaval帕特爾 – Ghost
@ghost您可以發佈錯誤日誌? –
這裏是鏈接:https://onedrive.live.com/redir?resid=CDB5011499C666C7!422&authkey=!AIVQ_oh-JIvTbGM&ithint=file%2c for logcat文件@Dhaval Patel – Ghost