0

我对操作栏很陌生,我想通过使用 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)
4

1 回答 1

0

这是我在 logcat 中遇到的错误

出现该错误是因为您尝试执行事务以将片段(Tab1fragment)添加到无法找到的视图(具有 id 的视图R.id.Fragment1)。我不知道您发布的是否是完整代码,但似乎带有 id 的视图R.id.Fragment1位于Tab1fragment的布局文件中,R.layout.fragmenttab. 如果是这种情况,请不要这样做,而是应该为已经在Activity. 例如,您可以使 的内容NextActivity变得简单FrameLayout,并将其用作选项卡内容的容器:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        FrameLayout fl = new FrameLayout(this);
    fl.setId(1000);
    setContentView(fl);
   //...

并将1000id 用于片段添加事务:

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

        if (check) {
            ft.add(1000, _fragment, null);
            check = false;
        } else {
            ft.attach(_fragment);
        }

    }
于 2013-01-19T10:35:25.013 回答