Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未定义中的getTabHost()方法_Java_Android - Fatal编程技术网

Java 未定义中的getTabHost()方法

Java 未定义中的getTabHost()方法,java,android,Java,Android,我正在用android制作标签。我正在使用TabHost来实现这一点。我在这一行的getTabHost()方法下得到了红线TabHost=getTabHost()。我不知道为什么我会得到这个。我怎样才能使这个例子起作用 这是我的完整代码 public class TabActivity extends Activity { //TabHost mTabHost; /** Called when the activity is first created. */ @Override public

我正在用android制作标签。我正在使用TabHost来实现这一点。我在这一行的
getTabHost()
方法下得到了红线
TabHost=getTabHost()。我不知道为什么我会得到这个。我怎样才能使这个例子起作用

这是我的完整代码

public class TabActivity extends Activity {
//TabHost mTabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    //Artist Tab
    intent = new Intent(this, Artist.class);
    spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
    tabHost.addTab(spec);

  //Songs
    intent = new Intent(this, Songs.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
    tabHost.addTab(spec);

  //Albums
    intent = new Intent(this, Album.class);
    spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(1);
}
}

您需要扩展MyTabActivity,而不是简单的activity 试一试

intead of

public class MyTabActivity extends Activity 

您想扩展android.app.TabActivity
,而不是
Activity
。请解决我的相关问题,我想在片段中实现TabHost,然后如何访问TabHost=getTabHost();,在这个语句中,getTabHost()是未定义的,因为我扩展了片段而不是TabActivity。那么如何在片段中实现TabHost。
public class MyTabActivity extends Activity