我对操作栏很新,我想使用min sdk 14和target sdk 16使用操作栏选项卡和片段进行简单的应用程序。
这是Fragment类
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)
答案 0 :(得分:0)
这是我在logcat中得到的错误
出现错误是因为您尝试执行事务以将片段(Tab1fragment
)添加到无法找到的视图(ID为R.id.Fragment1
的视图)。我不知道您发布的是完整代码,但似乎ID为R.id.Fragment1
的视图位于Tab1fragment
的布局文件R.layout.fragmenttab
中。如果是这种情况,请不要这样做,而应该为Activity
中已有的布局提供ID。例如,您可以将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);
//...
并使用1000
id作为片段添加事务:
@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);
}
}