我正在尝试创建一个标签视图,它将通过Id切换到不同的内容视图。这是我的tab activity
:
public class TabViews extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabviews);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabviews, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("apples")
.setIndicator("Apples")
.setContent(R.id.apples));
tabHost.addTab(tabHost.newTabSpec("events")
.setIndicator("Events")
.setContent(R.id.events));
tabHost.addTab(tabHost.newTabSpec("attend")
.setIndicator("Attending")
.setContent(R.id.attend));
}
}
这是tabviews xml布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"/>
<TextView android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"/>
<TextView android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"/>
</FrameLayout>
我有三个其他xml布局参加,活动和苹果,我可以通过在startActivity
上创建一个新的意图Intent
来访问。
这是我的R.java类:
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int bdgdefault=0x7f020000;
public static final int logo=0x7f020001;
public static final int black_0=0x7f020002;
public static final int black_1=0x7f020003;
public static final int black_2=0x7f020004;
public static final int black_3=0x7f020005;
public static final int black_4=0x7f020006;
public static final int blue_0=0x7f020007;
public static final int blue_1=0x7f020008;
public static final int blue_2=0x7f020009;
public static final int blue_3=0x7f02000a;
public static final int blue_4=0x7f02000b;
public static final int brown_0=0x7f02000c;
public static final int brown_1=0x7f02000d;
public static final int brown_2=0x7f02000e;
public static final int brown_3=0x7f02000f;
public static final int brown_4=0x7f020010;
public static final int btn_black=0x7f020011;
public static final int btn_black_1=0x7f020012;
public static final int btn_black_off=0x7f020013;
public static final int btn_dropdown_down=0x7f020014;
public static final int btn_dropdown_left_only=0x7f020015;
public static final int btn_dropdown_left_right_collapsed=0x7f020016;
public static final int btn_dropdown_neither=0x7f020017;
public static final int btn_dropdown_neither_up_down=0x7f020018;
public static final int btn_dropdown_right_only=0x7f020019;
public static final int btn_dropdown_up=0x7f02001a;
public static final int btn_dropdown_up_down=0x7f02001b;
public static final int btn_white=0x7f02001c;
public static final int button=0x7f02001d;
public static final int grac=0x7f02001e;
public static final int ic_launcher=0x7f02001f;
public static final int ic_menu_jitsu=0x7f020020;
public static final int ic_menu_jiu=0x7f020021;
public static final int ic_menu_name=0x7f020022;
public static final int mainlogo=0x7f020023;
public static final int purple_0=0x7f020024;
public static final int purple_1=0x7f020025;
public static final int purple_2=0x7f020026;
public static final int purple_3=0x7f020027;
public static final int purple_4=0x7f020028;
public static final int red_0=0x7f020029;
public static final int red_1=0x7f02002a;
public static final int mylogo=0x7f02002b;
public static final int spinner_press=0x7f02002c;
public static final int spinner_select=0x7f02002d;
public static final int white_0=0x7f02002e;
public static final int white_1=0x7f02002f;
public static final int white_2=0x7f020030;
public static final int white_3=0x7f020031;
public static final int white_4=0x7f020032;
}
public static final class id {
public static final int amattendees=0x7f060006;
public static final int attend=0x7f060013;
public static final int attendam=0x7f060000;
public static final int attendboth=0x7f060002;
public static final int attendpm=0x7f060001;
public static final int back=0x7f06000f;
public static final int bdg=0x7f06000b;
public static final int bdgBackground=0x7f060009;
public static final int btn_login=0x7f06001b;
public static final int btn_newuser=0x7f06001c;
public static final int description=0x7f060011;
public static final int details=0x7f06000c;
public static final int emailaddress=0x7f060019;
public static final int events=0x7f06000e;
public static final int grouplogo=0x7f060012;
public static final int mainlogo=0x7f060018;
public static final int nearestvenue=0x7f060016;
public static final int notattending=0x7f060003;
public static final int password=0x7f06001a;
public static final int playbutton=0x7f060014;
public static final int pmattendees=0x7f060007;
public static final int progressBar=0x7f06000d;
public static final int qrdecode=0x7f060015;
public static final int rewarddate=0x7f060010;
public static final int textView1=0x7f060017;
public static final int title=0x7f060008;
public static final int tv_am=0x7f060004;
public static final int tv_pm=0x7f060005;
public static final int view1=0x7f06001d;
public static final int view2=0x7f06001e;
public static final int view3=0x7f06001f;
public static final int wv_qr=0x7f06000a;
}
public static final class layout {
public static final int attend=0x7f030000;
public static final int apples=0x7f030001;
public static final int events=0x7f030002;
public static final int list_items_attendees=0x7f030003;
public static final int main=0x7f030004;
public static final int tabviews=0x7f030005;
}
public static final class string {
public static final int AM=0x7f040009;
public static final int Login=0x7f040002;
public static final int Update=0x7f04000a;
public static final int am=0x7f04000d;
public static final int amattendees=0x7f040011;
public static final int app_name=0x7f040001;
public static final int attend_both=0x7f04000f;
public static final int attend_pm=0x7f04000e;
public static final int back=0x7f040005;
public static final int badge_details=0x7f040006;
public static final int email_text=0x7f040007;
public static final int empty=0x7f04000c;
public static final int events=0x7f040004;
public static final int hello=0x7f040000;
public static final int new_user=0x7f040003;
public static final int not_attending=0x7f040010;
public static final int password_text=0x7f040008;
public static final int pmattendees=0x7f040012;
public static final int qrdecode=0x7f04000b;
}
public static final class style {
public static final int ButtonText=0x7f050000;
}
}
以下是我收到的错误:
04-17 10:14:48.608: E/AndroidRuntime(11307):java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jujitsu.app.com/org.jujitsu.app.com.TabViews}:java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099659
更多:
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.os.Looper.loop(Looper.java:143)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.ActivityThread.main(ActivityThread.java:4196)
04-17 10:14:48.608: E/AndroidRuntime(11307): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 10:14:48.608: E/AndroidRuntime(11307): at java.lang.reflect.Method.invoke(Method.java:507)
04-17 10:14:48.608: E/AndroidRuntime(11307): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-17 10:14:48.608: E/AndroidRuntime(11307): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-17 10:14:48.608: E/AndroidRuntime(11307): at dalvik.system.NativeStart.main(Native Method)
04-17 10:14:48.608: E/AndroidRuntime(11307): Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099659
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:595)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:586)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.widget.TabHost$TabSpec.setContent(TabHost.java:441)
04-17 10:14:48.608: E/AndroidRuntime(11307): at org.jujitsu.app.com.TabViews.onCreate(TabViews.java:26)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-17 10:14:48.608: E/AndroidRuntime(11307): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
04-17 10:14:48.608: E/AndroidRuntime(11307): ... 11 more
不确定我做错了什么。我对此有点新鲜。
更新:好的..我的意思是..我想使用标签更改活动。继承人就是这样做的。答案是通过Intent设置内容,而不是使用tabHost.addTab(tabHost.newTabSpec(“tab2”)。setIndicator(“Something”)。setContent(new Intent(this,myActivity.class)));
谢谢所有帮助过的人。这让我考虑了更多的选择。
干杯,乔。
答案 0 :(得分:2)
你的xml应该有类似的东西,
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"/>
<TextView android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"/>
<TextView android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"/>
</FrameLayout>
</TabHost>
例子: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/ http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android
答案 1 :(得分:1)
您的xml布局应该有TabHost
和TabWidget
,请查看Tab Layout example。
答案 2 :(得分:0)
你需要在xml中添加tabwidget .... Checkout,这是如何使用tab小部件:
tabscroll.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
public class Tabs5 extends TabActivity implements TabHost.TabContentFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_scroll);
final TabHost tabHost = getTabHost();
for (int i=1; i <= 30; i++) {
String name = "Tab " + i;
tabHost.addTab(tabHost.newTabSpec(name)
.setIndicator(name)
.setContent(this));
}
}
/** {@inheritDoc} */
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return tv;
}
}
礼貌:http://developer.android.com/resources/samples/ApiDemos/res/layout/tabs_scroll.html AND http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs5.html