Tabhost 选项卡 onCreate 方法被调用两次
似乎当我单击选项卡按钮时, onCreate 方法被调用两次。还有其他方法可以制作 tabhost 吗?这是我的代码:
public class MyTabHostActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabactivity);
TabHost tabHost=getTabHost();
setup(tabHost);
}
private void setup(TabHost tabHost)
{
Intent intent1 = new Intent(this, Activity1.class);
setupTab("Activity1", tabHost, intent1);
Intent intent2 = new Intent(this,Activity2.class);
setupTab("Activity2", tabHost, intent2);
Intent intent3 = new Intent(this,Activity3.class);
setupTab("Activity3", tabHost, intent3);
}
private void setupTab(final String tag, final TabHost mTabHost, Intent intent) {
View tabview = createTabView(mTabHost.getContext(), tag, mTabHost);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text, final TabHost mTabHost) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
public class Activity1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Activity1", "onCreate");
}
}
public class Activity2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Activity2", "onCreate");
}
}
public class Activity3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Activity3", "onCreate");
}
}
tabactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android">
<TabWidget android:layout_width="fill_parent" android:layout_gravity="bottom"
android:layout_height="wrap_content" android:id="@android:id/tabs" />
<FrameLayout android:layout_width="fill_parent" android:paddingBottom="45dp"
android:layout_height="wrap_content" android:id="@android:id/tabcontent">
</FrameLayout>
</TabHost>
tab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip" />
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTabHostActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity1" android:screenOrientation="portrait" />
<activity android:name=".Activity2" android:screenOrientation="portrait" />
<activity android:name=".Activity3" android:screenOrientation="portrait" />
</application>
</manifest>
It seems when I click on my tab button, the onCreate method gets called twice. Is there another way to make a tabhost? heres my code:
public class MyTabHostActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabactivity);
TabHost tabHost=getTabHost();
setup(tabHost);
}
private void setup(TabHost tabHost)
{
Intent intent1 = new Intent(this, Activity1.class);
setupTab("Activity1", tabHost, intent1);
Intent intent2 = new Intent(this,Activity2.class);
setupTab("Activity2", tabHost, intent2);
Intent intent3 = new Intent(this,Activity3.class);
setupTab("Activity3", tabHost, intent3);
}
private void setupTab(final String tag, final TabHost mTabHost, Intent intent) {
View tabview = createTabView(mTabHost.getContext(), tag, mTabHost);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text, final TabHost mTabHost) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
public class Activity1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Activity1", "onCreate");
}
}
public class Activity2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Activity2", "onCreate");
}
}
public class Activity3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Activity3", "onCreate");
}
}
tabactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android">
<TabWidget android:layout_width="fill_parent" android:layout_gravity="bottom"
android:layout_height="wrap_content" android:id="@android:id/tabs" />
<FrameLayout android:layout_width="fill_parent" android:paddingBottom="45dp"
android:layout_height="wrap_content" android:id="@android:id/tabcontent">
</FrameLayout>
</TabHost>
tab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip" />
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTabHostActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity1" android:screenOrientation="portrait" />
<activity android:name=".Activity2" android:screenOrientation="portrait" />
<activity android:name=".Activity3" android:screenOrientation="portrait" />
</application>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不会被调用两次。我非常确定您在
模拟器
中运行代码,并且您会看到该方法被触发两次。但这只是模拟器
中的另一个错误,在真实设备上永远不会发生。It does not get called twice. I'm pretty sure you run your code in your
emulator
and you see the method being fired twice. But this is just another bug in theemulator
and this will never happen on a real device.