作为一名初学者,我想学习使用tabhost / tabwidget的android标签布局,不过我已经阅读了一些已经弃用的帖子。所以...基本上,我想要实现的是拥有一个带有两个选项卡的布局,这两个选项卡都将显示每个具有不同内容的webview。但每当我尝试访问该布局时,我的应用程序都会崩溃。
以下是我在Logcat中所说的内容:
logcat的:
04-09 22:30:53.628: E/AndroidRuntime(10406): FATAL EXCEPTION: main
04-09 22:30:53.628: E/AndroidRuntime(10406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicx.amazing/com.nicx.amazing.TabActivity}: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.os.Looper.loop(Looper.java:137)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-09 22:30:53.628: E/AndroidRuntime(10406): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-09 22:30:53.628: E/AndroidRuntime(10406): at dalvik.system.NativeStart.main(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): Caused by: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.support.v4.app.FragmentManagerImpl.attachActivity(FragmentManager.java:1844)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:193)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.nicx.amazing.TabActivity.onCreate(TabActivity.java:15)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.Activity.performCreate(Activity.java:4465)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-09 22:30:53.628: E/AndroidRuntime(10406): ... 11 more
04-09 22:30:53.628: E/AndroidRuntime(10406): FATAL EXCEPTION: main
04-09 22:30:53.628: E/AndroidRuntime(10406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicx.amazing/com.nicx.amazing.TabActivity}: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.os.Looper.loop(Looper.java:137)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-09 22:30:53.628: E/AndroidRuntime(10406): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-09 22:30:53.628: E/AndroidRuntime(10406): at dalvik.system.NativeStart.main(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): Caused by: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.support.v4.app.FragmentManagerImpl.attachActivity(FragmentManager.java:1844)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:193)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.nicx.amazing.TabActivity.onCreate(TabActivity.java:15)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.Activity.performCreate(Activity.java:4465)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-09 22:30:53.628: E/AndroidRuntime(10406): ... 11 more
这是我在TabActivity.java中的内容:
注意:我仍然没有想出如何在我的标签中放置webview,我只想先看看我的标签的布局。import android.os.Bundle;
import android.support.v4.app.*;
import android.widget.TabHost;
public class TabActivity extends FragmentActivity {
TabHost tHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tHost = (TabHost) findViewById(android.R.id.tabhost);
tHost.setup();
TabHost.TabSpec fb = tHost.newTabSpec("facebook");
fb.setIndicator("Facebook", null);
fb.setContent(new DummyTabContent(getBaseContext()));
tHost.addTab(fb);
TabHost.TabSpec tw = tHost.newTabSpec("twitter");
tw.setIndicator("Twitter", null);
tw.setContent(new DummyTabContent(getBaseContext()));
tHost.addTab(tw);
}
}
我的activity_tab.xml中也有这个:
import android.os.Bundle;
import android.support.v4.app.*;
import android.widget.TabHost;
public class TabActivity extends FragmentActivity {
TabHost tHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tHost = (TabHost) findViewById(android.R.id.tabhost);
tHost.setup();
TabHost.TabSpec fb = tHost.newTabSpec("facebook");
fb.setIndicator("Facebook", null);
fb.setContent(new DummyTabContent(getBaseContext()));
tHost.addTab(fb);
TabHost.TabSpec tw = tHost.newTabSpec("twitter");
tw.setIndicator("Twitter", null);
tw.setContent(new DummyTabContent(getBaseContext()));
tHost.addTab(tw);
}
}
我使用http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/作为指南。因为我没有使用它,所以我没有遵循它的碎片。你能帮我解决这个问题吗?
答案 0 :(得分:0)
哦,亲爱的,Facebook和Twitter也必须有两个活动,你会把它们抛在脑后吗?
您必须创建这些活动才能完成您的应用。
我发现你的TabActivity扩展了FrameActivity,这似乎不对。 您的MainActivity应该扩展TabActivity。
这是我的一个考试
package com.izzyleung.mixedbartest;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
Intent imageIntent = new Intent();
imageIntent.setClass(this, ImageActivity.class);
TabHost.TabSpec imageSpec = tabHost.newTabSpec("Image");
imageSpec.setIndicator("Image");
imageSpec.setContent(imageIntent);
tabHost.addTab(imageSpec);
Intent notificationIntent = new Intent();
notificationIntent.setClass(this, NotificationActivity.class);
TabHost.TabSpec notificationSpec = tabHost.newTabSpec("Notification");
notificationSpec.setIndicator("Notification");
notificationSpec.setContent(notificationIntent);
tabHost.addTab(notificationSpec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Intent settingsIntent = new Intent();
settingsIntent.setClass(this, SettingsActivity.class);
startActivity(settingsIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我的XML文件是这样的:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5sp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5sp" >
</FrameLayout>
</LinearLayout>