我有一个非常简单的应用程序,它只是一个带有标签视图的活动。
我已经初始化并将所有内容转换为应有的但是我不断得到一个空指针错误,它始终链接回
tabHost.setup();
我正在使用android studio并且是java的新手。这个问题在这里已经被问了很多,但所有的答案只是说包括setup(),我已经做过了。
这是我的.java文件:
package com.example.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.TabHost;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1, spec2, spec3;
spec1 = tabHost.newTabSpec("spec1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab1");
tabHost.addTab(spec1);
spec2 = tabHost.newTabSpec("spec2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab2");
tabHost.addTab(spec2);
spec3 = tabHost.newTabSpec("spec3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Tab3");
tabHost.addTab(spec3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
我的代码和一些在线教程之间的唯一区别是公共类MainActivity扩展到ActionBarActivity而不仅仅是Activity。我甚至没有这样做,当我创建一个空白项目时它是默认的。
XML文件也在下面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.app.MainActivity$PlaceholderFragment">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
答案 0 :(得分:2)
此行显示您创建的TabHost位于布局目录中的fragment_main.xml
内,PlaceholderFragment使用
tools:context="com.example.app.MainActivity$PlaceholderFragment"
但您在activity_main.xml
将代码从onCreate()
移至onCreateView
的PlaceholderFragment,如果您使用默认模板附带AS,请在同一类下面
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TabHost tabHost = (TabHost) rootView.findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1, spec2, spec3;
spec1 = tabHost.newTabSpec("spec1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab1");
tabHost.addTab(spec1);
spec2 = tabHost.newTabSpec("spec2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab2");
tabHost.addTab(spec2);
spec3 = tabHost.newTabSpec("spec3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Tab3");
tabHost.addTab(spec3);
return rootView;
}
或者如果你想要你可以将你的TabHost移动到activity_main.xml
而不改变java代码,但不建议使用具有很多好处的片段。
检查这是否有使用碎片的好处
答案 1 :(得分:-1)
这是我使用TabHost的代码,但现在我改用了片段。
public class MainActivity extends TabActivity {
private TabHost tabHost;
private TabHost.TabSpec spec;
@SuppressWarnings("unused")
private Resources res;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_new);
//Test Button
Button testBtn = (Button)findViewById(R.id.title_imagebtn);
testBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog();
}
});
this.res = getResources();
this.tabHost = getTabHost();
Intent intent = new Intent().setClass(this, LearnResActivity.class);
this.spec = this.tabHost
.newTabSpec(getString(R.string.res_learn))
.setIndicator(
getString(R.string.res_learn),
getResources().getDrawable(
android.R.drawable.ic_media_play))
.setContent(intent);
this.tabHost.addTab(this.spec);
this.tabHost = getTabHost();
intent = new Intent().setClass(this, MyLearnActivity.class);
this.spec = this.tabHost
.newTabSpec(getResources().getString(R.string.my_learn))
.setIndicator(
getString(R.string.my_learn),
getResources().getDrawable(
android.R.drawable.ic_menu_recent_history))
.setContent(intent);
this.tabHost.addTab(this.spec);
this.tabHost = getTabHost();
intent = new Intent().setClass(this, MyTestActivity.class);
this.spec = this.tabHost
.newTabSpec(getString(R.string.my_test))
.setIndicator(
getString(R.string.my_test),
getResources().getDrawable(
android.R.drawable.ic_menu_edit))
.setContent(intent);
this.tabHost.addTab(this.spec);
}}