当前位置:编程学习 > wap >>

关于TabHost addTab的问题

在代码中,使用的是FrangmentActicity,因为TabActivity已经不能用了,可是每次运行时,应用程序都无法启动,我试了下,好像到了spec = tabhost.newTabSpec("rb_player_local").setIndicator("rb_player_local").setContent(new Intent(this, Test.class));
这句时,程序就执行不下去了,因为后面这句System.out.println("tabhost");一直就没有执行,求各位大神指导指导


import android.os.Bundle;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;


public class MainActivity extends FragmentActivity implements OnCheckedChangeListener{

private TabHost tabhost;
private RadioGroup rg_player_control;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost_list);
//绑定tabhost
tabhost = (TabHost)findViewById(android.R.id.tabhost);
tabhost.setup();

//radiogroup操作
    rg_player_control = (RadioGroup)findViewById(R.id.music_tab_group);
    
    rg_player_control.setOnCheckedChangeListener(this);
    rg_player_control.setClickable(true);

    TabHost.TabSpec spec;
    spec = tabhost.newTabSpec("rb_player_local").setIndicator("rb_player_local").setContent(new Intent(this, Test.class));
    System.out.println("tabhost");
    tabhost.addTab(spec);
    
    spec = tabhost.newTabSpec("rb_player_favorite").setIndicator("rb_player_favorite").setContent(new Intent(this, Test2.class));
    tabhost.addTab(spec);
    spec = tabhost.newTabSpec("rb_player_online").setIndicator("rb_player_online").setContent(new Intent(this, Test3.class));
    tabhost.addTab(spec);
}


@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub

switch(checkedId)
{
case R.id.rb_player_local:
tabhost.setCurrentTabByTag("rb_player_local");
break;
case R.id.rb_player_favorite:
tabhost.setCurrentTabByTag("rb_player_favorite");
break;
case R.id.rb_player_online:
tabhost.setCurrentTabByTag("rb_player_online");
break;
}

}

}
androidtabhost  --------------------编程问答--------------------
引用 楼主 kaka_answer 的回复:
在代码中,使用的是FrangmentActicity,因为TabActivity已经不能用了,可是每次运行时,应用程序都无法启动,我试了下,好像到了spec = tabhost.newTabSpec("rb_player_local").setIndicator("rb_player_local").setContent(new Intent(this, Test.class));
这句时,程序就执行不下去了

这一句中可能出问题的就是new Intent(this, Test.class)
看看AndroidManifest.xml文件中有没有注册Test这个Activity。 --------------------编程问答-------------------- 我加了的,可是还是不行啊。。。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mymusic"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mymusic.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>
        <activity 
            android:name="com.example.mymusic.Test"
            android:label="@string/app_name" >
            
        </activity>
                <activity 
            android:name="com.example.mymusic.Test2"
            android:label="@string/app_name" >
            
        </activity>
                <activity 
            android:name="com.example.mymusic.Test3"
            android:label="@string/app_name" >
            
        </activity>
    </application>

</manifest>
--------------------编程问答-------------------- ????
求指导啊 --------------------编程问答-------------------- 抱歉,好长时间没上网。
那不是这个问题,不妨贴Log出来看看。 --------------------编程问答-------------------- mTabHost = getTabHost(); 这么获取你那个 tabHost 试试看 --------------------编程问答-------------------- 错了   我是用的这个 TabActivity  --------------------编程问答-------------------- lz的问题解决了吗,没解决的话把log贴出来给大家看看啊,我以前也用的tabActivity
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,