我有一个带有 3 个 xml 布局文件的选项卡式应用程序。片段以编程方式创建并添加到 LinearLayouts。
但是,其中一个 LinearLayouts 看起来像这样:
地图.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/primary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YOLO" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/secondary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
此选项卡应分为两种布局,一种占屏幕的 25%,另一种占 75%。
这使我的 TabListener 无法替换整个 .xml 文件,而我只能替换secondary
LinearLayout,因为这是我在onCreate
活动方法期间添加片段的地方。
因此,我最终得到 25% 的应用程序顶部是静态的并且永远不会改变。
当我尝试替换父布局map
时,应用程序崩溃。
我能做些什么?
package com.nfc.demo;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
public class NFCDemoActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_SATELLITE).compassEnabled(false)
.rotateGesturesEnabled(false).tiltGesturesEnabled(false);
MapFragment mMapFragment = MapFragment.newInstance(options);
Fragment mSettingsFragment = new SettingsFragment();
Fragment mAboutFragment = new AboutFragment();
ft.add(R.id.secondary, mMapFragment);
ft.commit();
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
ActionBar.Tab mapTab = bar.newTab().setText("Map");
ActionBar.Tab settingsTab = bar.newTab().setText("Settings");
ActionBar.Tab aboutTab = bar.newTab().setText("About");
mapTab.setTabListener(new TabListener(mMapFragment));
settingsTab.setTabListener(new TabListener(mSettingsFragment));
aboutTab.setTabListener(new TabListener(mAboutFragment));
bar.addTab(mapTab, 0);
bar.addTab(settingsTab, 1);
bar.addTab(aboutTab, 2);
setContentView(R.layout.map);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
protected class TabListener implements ActionBar.TabListener {
private Fragment fragment;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(Tab tab,
FragmentTransaction fragmentTransaction) {
fragmentTransaction.replace(R.id.secondary, this.fragment, null);
}
public void onTabUnselected(Tab tab,
FragmentTransaction fragmentTransaction) {
fragmentTransaction.remove(this.fragment);
}
public void onTabReselected(Tab tab,
FragmentTransaction fragmentTransaction) {
// do nothing
}
}
}
尝试替换 R.id.map 而不是替换 R.id.secondary 时的错误日志
12-30 14:01:16.035: E/AndroidRuntime(12377): FATAL EXCEPTION: main
12-30 14:01:16.035: E/AndroidRuntime(12377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nfc.demo/com.nfc.demo.NFCDemoActivity}: java.lang.IllegalStateException: Can't change container ID of fragment MapFragment{41ca1660 id=0x7f04000c}: was 2130968588 now 2130968585
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.os.Looper.loop(Looper.java:137)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 14:01:16.035: E/AndroidRuntime(12377): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 14:01:16.035: E/AndroidRuntime(12377): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 14:01:16.035: E/AndroidRuntime(12377): at dalvik.system.NativeStart.main(Native Method)
12-30 14:01:16.035: E/AndroidRuntime(12377): Caused by: java.lang.IllegalStateException: Can't change container ID of fragment MapFragment{41ca1660 id=0x7f04000c}: was 2130968588 now 2130968585
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.BackStackRecord.doAddOp(BackStackRecord.java:407)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.BackStackRecord.replace(BackStackRecord.java:429)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.nfc.demo.NFCDemoActivity$TabListener.onTabSelected(NFCDemoActivity.java:77)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:509)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:490)
12-30 14:01:16.035: E/AndroidRuntime(12377): at com.nfc.demo.NFCDemoActivity.onCreate(NFCDemoActivity.java:47)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.Activity.performCreate(Activity.java:5104)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-30 14:01:16.035: E/AndroidRuntime(12377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-30 14:01:16.035: E/AndroidRuntime(12377): ... 11 more