我对操作栏很陌生,我想通过使用 min sdk 14 和 target sdk 16 使用操作栏选项卡和片段制作简单的应用程序。
这是片段类
public class Tab1fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragmenttab, container,false);
}
}
这是tablistener类
public class tabslistener implements ActionBar.TabListener{
Fragment _fragment;
boolean check=true;
public tabslistener(Fragment _fragment)
{
this._fragment=_fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
if(check)
{
ft.add(R.id.Fragment1, _fragment,null);
check=false;
}
else
{
ft.attach(_fragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.detach(_fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
这是我的主要课程
public class NextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ActionBar actionbar=getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Fragment fragment=Fragment.instantiate(this, Tab1fragment.class.getName());
tabslistener tablistener=new tabslistener(fragment);
ActionBar.Tab tab1=actionbar.newTab().setTabListener(tablistener);
actionbar.addTab(tab1);
}
这是我在 logcat 中遇到的错误
01-19 01:40:40.078: E/AndroidRuntime(725): FATAL EXCEPTION: main
01-19 01:40:40.078: E/AndroidRuntime(725): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabs/com.example.tabs.NextActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070001 for fragment Tab1fragment{416511c8 #0 id=0x7f070001}
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.os.Looper.loop(Looper.java:137)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-19 01:40:40.078: E/AndroidRuntime(725): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 01:40:40.078: E/AndroidRuntime(725): at java.lang.reflect.Method.invoke(Method.java:511)
01-19 01:40:40.078: E/AndroidRuntime(725): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-19 01:40:40.078: E/AndroidRuntime(725): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-19 01:40:40.078: E/AndroidRuntime(725): at dalvik.system.NativeStart.main(Native Method)
01-19 01:40:40.078: E/AndroidRuntime(725): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f070001 for fragment Tab1fragment{416511c8 #0 id=0x7f070001}
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:822)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.BackStackRecord.run(BackStackRecord.java:622)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.Activity.performStart(Activity.java:4474)
01-19 01:40:40.078: E/AndroidRuntime(725): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)