Tabhost 选项卡 onCreate 方法被调用两次

发布于 2024-12-04 21:29:50 字数 4597 浏览 0 评论 0原文

似乎当我单击选项卡按钮时, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-12-11 21:29:50

它不会被调用两次。我非常确定您在模拟器中运行代码,并且您会看到该方法被触发两次。但这只是模拟器中的另一个错误,在真实设备上永远不会发生。

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 the emulator and this will never happen on a real device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文