我已经使用片段创建了一个选项卡式窗口,我现在在其中一个选项卡中有三个选项卡我需要更改内容而不更改选项卡,我搜索并提出以下内容:
public class FragmentTab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab1, container, false);
Button b = (Button)rootView.findViewById(R.id.button1);
b.setOnClickListener(on);
return rootView;
}
private View.OnClickListener on = new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
Fragment3 fragment3 = new Fragment3();
fragmentTransaction.replace(R.layout.fragmenttab1, fragment3);
fragmentTransaction.commit();
}
};
}
这是我的主要活动:
public class MainActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setIcon(R.drawable.tab1);
Tab1 = actionBar.newTab().setText("Reminders");
//Tab2 = actionBar.newTab().setIcon(R.drawable.tab1);
Tab2 = actionBar.newTab().setText("Notifications");
//Tab3 = actionBar.newTab().setIcon(R.drawable.tab1);
Tab3 = actionBar.newTab().setText("Contacts");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
}
这是片段类:
public class Fragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab3, container, false);
return rootView;
}
}
然而,当我运行这个程序时,我收到一个奇怪的错误:
12-03 17:51:20.166: E/AndroidRuntime(1168): FATAL EXCEPTION: main
12-03 17:51:20.166: E/AndroidRuntime(1168): java.lang.IllegalArgumentException: No view found for id 0x7f030002 for fragment Fragment3{4155bdb8 #1 id=0x7f030002}
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:789)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.app.BackStackRecord.run(BackStackRecord.java:622)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.os.Handler.handleCallback(Handler.java:605)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.os.Looper.loop(Looper.java:137)
12-03 17:51:20.166: E/AndroidRuntime(1168): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-03 17:51:20.166: E/AndroidRuntime(1168): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 17:51:20.166: E/AndroidRuntime(1168): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 17:51:20.166: E/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-03 17:51:20.166: E/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-03 17:51:20.166: E/AndroidRuntime(1168): at dalvik.system.NativeStart.main(Native Method)
我可能会出错的任何指示?
答案 0 :(得分:1)
在您的代码中没有发现任何问题。
布局有问题。请检查片段布局和主要活动布局。