我必须创建两个tabs.And“TabHost tabHost = getTabHost();”给我一个错误,说方法getTabHost()在MainActivity中是unfine。
我的mainActivity.java文件: 包com.example.bckup;
import android.os.Bundle;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.content.Context;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, line1.class);
TabSpec tabSpecAndroid = tabHost.newTabSpec("Android")
.setContent(intentAndroid);
//Linux tab
Intent intentLinux = new Intent().setClass(this, line2.class);
TabSpec tabSpecLinux = tabHost.newTabSpec("Linux")
.setContent(intentLinux);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecLinux);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
}
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
*/
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class line1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_list);
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.friends_list, menu);
return true;
}*/
}
acivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
任何人都可以帮我解决一下。
答案 0 :(得分:2)
您的主要活动应扩展 TabActivity
public class MainActivity extends TabActivity
答案 1 :(得分:0)
使用
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
而不是
TabHost tabHost = getTabHost();
并扩展TabActivity