问题描述
- 关于通过fragment定义选项卡的问题
-
布局文件如下:<?xml version="1.0" encoding="utf-8"?>
package="net.tycmc.fragementtabhost"
android:versionCode="1"
android:versionName="1.0" ><uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
主 文件如下:
package net.tycmc.fragementtabhost;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost.TabSpec;public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost = null;
private Class[]targetClass={
OneFragment.class,TwoFragment.class,ThreeFragment.class,FourFragment.class};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// TODO Auto-generated method stub
mTabHost=(FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(),R.id.framelayout_main);
for(int i=0;i<targetClass.length;i++){
TabSpec tabspec=mTabHost.newTabSpec(i+"").setIndicator("第"+i+"个");
//此注释俩行是 迷惑部分
/*tabspec.setContent(new Intent(MainActivity.this,targetClass[i]));
mTabHost.addTab(tabspec);*/
mTabHost.addTab(tabspec,targetClass[i],null);
}
}}
当前程序是没有问题,正常显示的,但是当把最后一行注释,把倒数第二,第三行放开,反而出错了,我很困惑。按理说应该一样啊。求高手解答。
解决方案
http://blog.csdn.net/zjlovety/article/details/21519205