Tabhost NullPointerException异常时tabhost是不是在布局的唯一的事 [英] Tabhost nullpointerexception when tabhost isn't the only thing in the layout
问题描述
我注意到,有使用tabhost许多问题,但没有人真正适合我的问题。我想创建与顶部的行动起来吧,然后根据标签的排期酒吧。这里是我的活动布局。
<?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
风格=@风格/仪表板> < com.ftni.common.ui.ActionBar
机器人:ID =@ + ID /动作条
风格=@风格/动作条/> < TabHost机器人:ID =@机器人:ID / tabhost
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<的LinearLayout
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:填充=5DP>
< TabWidget
机器人:ID =@机器人:ID /标签
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
<的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:填充=5DP/>
< / LinearLayout中>
< / TabHost>
< / LinearLayout中>
接下来,我们已经宣布为我的活动
公共类DashboardActivity扩展TabActivity {
和我建立我的标签是这样的。
私人无效buildTabs()
{
资源解析度= getResources(); //资源对象获取可绘制
TabHost tabHost =(TabHost)findViewById(android.R.id.tabhost); //活动TabHost
TabHost.TabSpec规范; // Resusable则tabspec为每个标签
意图意图; //可重用的意图为每个标签 //创建一个Intent来启动一个活动的标签(重复使用)
意图=新意图()setClass(这一点,StatementsActivity.class)。 //初始化则tabspec每个选项卡,并把它添加到TabHost
规格= tabHost.newTabSpec(声明)。setIndicator(声明,
res.getDrawable(R.drawable.ic_tab_active))
.setContent(意向);
tabHost.addTab(规范); //做其他选项卡相同
意图=新意图()setClass(这一点,PaymentsActivity.class)。
规格= tabHost.newTabSpec(金)。setIndicator(付款,
res.getDrawable(R.drawable.ic_tab_active))
.setContent(意向);
tabHost.addTab(规范); tabHost.setCurrentTab(0);
}
当我试图使用 getTabHost()
它总是返回一个空。所以,我切换到 TabHost tabHost =(TabHost)findViewById(android.R.id.tabhost);
不返回空tabhost,但 tabHost.addTab(规范);
还是引起了空指针异常
这里是堆栈跟踪
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):致命异常:主要
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.ftni.consumer / com.ftni.consumer.ui.DashboardActivity}:显示java.lang.NullPointerException
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:928)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.os.Handler.dispatchMessage(Handler.java:99)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.os.Looper.loop(Looper.java:123)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.ActivityThread.main(ActivityThread.java:3647)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在java.lang.reflect.Method.invokeNative(本机方法)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在java.lang.reflect.Method.invoke(Method.java:507)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在dalvik.system.NativeStart.main(本机方法)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):因:显示java.lang.NullPointerException
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.widget.TabHost.addTab(TabHost.java:212)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在com.ftni.consumer.ui.DashboardActivity.buildTabs(DashboardActivity.java:75)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在com.ftni.consumer.ui.DashboardActivity.onCreate(DashboardActivity.java:29)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
04-26 19:50:12.494:ERROR / AndroidRuntime(7367):11 ...更多
75行是第一个 tabHost.addTab(规范);
另外,我所有的活动都在我的清单中声明。
<活动机器人:名字=。ui.DashboardActivity>< /活性GT;
<活动机器人:名字=。ui.StatementsActivity>< /活性GT;
<活动机器人:名字=。ui.PaymentsActivity>< /活性GT;
编辑:
只是为了证明这不是一个问题,我删除了动作条,把布局到底是什么在本教程中,我仍然得到一个空的 getTabHost(部分);
编辑2:
满级code为清楚起见
公共类DashboardActivity扩展TabActivity {
私人ProfileModel轮廓; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 简介= Controller.getProfile(); 的setContentView(R.layout.dashboard); buildTabs(); 新LoadDashboardTask()执行();
} @覆盖
公共无效onContentChanged()
{
动作条的ActionBar =(动作条)findViewById(R.id.actionbar);
如果(动作条!= NULL)
{
actionBar.setOnTitleClickListener(新OnClickListener(){
公共无效的onClick(视图v){
//现在什么也不做。用户已经是家
}
}); 如果(profile.Accounts.length == 1)
actionBar.setTitle(profile.Accounts [0] .Name点);
其他
actionBar.setTitle(profile.Name); }
} 私人无效buildTabs()
{
资源解析度= getResources(); //资源对象获取可绘制
TabHost tabHost = getTabHost(); //活动TabHost 如果(tabHost!= NULL)
{
//初始化则tabspec每个选项卡,并把它添加到TabHost
则tabspec statementSpec = tabHost.newTabSpec(声明)。setIndicator(声明,
res.getDrawable(R.drawable.ic_tabs_statements))
.setContent(新意图()setClass(DashboardActivity.this,StatementsActivity.class));
tabHost.addTab(statementSpec); 则tabspec paymentSpec = tabHost.newTabSpec(金)。setIndicator(付款,
res.getDrawable(R.drawable.ic_tabs_payments))
.setContent(新意图()setClass(DashboardActivity.this,PaymentsActivity.class));
tabHost.addTab(paymentSpec); tabHost.setCurrentTab(0);
}
} 私有类LoadDashboardTask扩展的AsyncTask<太虚,太虚,太虚> {
@覆盖
在preExecute保护无效()
{
动作条的ActionBar =(动作条)findViewById(R.id.actionbar);
actionBar.setProgressBarVisibility(View.VISIBLE);
} @覆盖
保护无效doInBackground(无效... PARAMS)
{
返回null;
} @覆盖
保护无效onPostExecute(虚空结果)
{
动作条的ActionBar =(动作条)findViewById(R.id.actionbar);
actionBar.setProgressBarVisibility(View.GONE);
}
}
}
文档指出你应该,这是你在做什么,并称如果标签加载使用findViewById()。TabHost之前建立呼叫()。这里请参见。然而,由于您使用的是TabActivity,你为什么不叫 getTabHost()
,而不是手动获取看法?
I've noticed that there are numerous issues with using tabhost, but none of them really fit my issue. I am trying to create a view with an action bar on top, then a row of tabs under the bar. Here's my activity layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/Dashboard">
<com.ftni.common.ui.ActionBar
android:id="@+id/actionbar"
style="@style/ActionBar"/>
<TabHost android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
Next, we have my activity declared as such
public class DashboardActivity extends TabActivity {
and I am building my tabs like this.
private void buildTabs()
{
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, StatementsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("statements").setIndicator("Statements",
res.getDrawable(R.drawable.ic_tab_active))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, PaymentsActivity.class);
spec = tabHost.newTabSpec("payments").setIndicator("Payments",
res.getDrawable(R.drawable.ic_tab_active))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
when I tried using getTabHost();
it was always returning a null. So, I switched to TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
which does not return a null tabhost, but tabHost.addTab(spec);
still causes a null pointer exception.
And here is the stacktrace
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): FATAL EXCEPTION: main
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ftni.consumer/com.ftni.consumer.ui.DashboardActivity}: java.lang.NullPointerException
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.os.Looper.loop(Looper.java:123)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at java.lang.reflect.Method.invoke(Method.java:507)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at dalvik.system.NativeStart.main(Native Method)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): Caused by: java.lang.NullPointerException
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.widget.TabHost.addTab(TabHost.java:212)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.ftni.consumer.ui.DashboardActivity.buildTabs(DashboardActivity.java:75)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.ftni.consumer.ui.DashboardActivity.onCreate(DashboardActivity.java:29)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): ... 11 more
Line 75 is the first tabHost.addTab(spec);
Also, all my activities are declared in my manifest.
<activity android:name=".ui.DashboardActivity"></activity>
<activity android:name=".ui.StatementsActivity"></activity>
<activity android:name=".ui.PaymentsActivity"></activity>
EDIT:
Just to prove it isn't an issue, I removed the actionbar, put the layout to exactly what is in the tutorial, and I still get a null returned from getTabHost();
EDIT 2: full class code for clarity
public class DashboardActivity extends TabActivity {
private ProfileModel profile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
profile = Controller.getProfile();
setContentView(R.layout.dashboard);
buildTabs();
new LoadDashboardTask().execute();
}
@Override
public void onContentChanged()
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
if (actionBar != null)
{
actionBar.setOnTitleClickListener(new OnClickListener() {
public void onClick(View v) {
//do nothing for now. User is already "home"
}
});
if(profile.Accounts.length == 1)
actionBar.setTitle(profile.Accounts[0].Name);
else
actionBar.setTitle(profile.Name);
}
}
private void buildTabs()
{
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();// The activity TabHost
if(tabHost != null)
{
// Initialize a TabSpec for each tab and add it to the TabHost
TabSpec statementSpec = tabHost.newTabSpec("statements").setIndicator("Statements",
res.getDrawable(R.drawable.ic_tabs_statements))
.setContent(new Intent().setClass(DashboardActivity.this, StatementsActivity.class));
tabHost.addTab(statementSpec);
TabSpec paymentSpec = tabHost.newTabSpec("payments").setIndicator("Payments",
res.getDrawable(R.drawable.ic_tabs_payments))
.setContent(new Intent().setClass(DashboardActivity.this, PaymentsActivity.class));
tabHost.addTab(paymentSpec);
tabHost.setCurrentTab(0);
}
}
private class LoadDashboardTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute()
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
actionBar.setProgressBarVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params)
{
return null;
}
@Override
protected void onPostExecute(Void result)
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
actionBar.setProgressBarVisibility(View.GONE);
}
}
}
The documentation states that you should "Call setup() before adding tabs if loading TabHost using findViewById().", which is what you're doing. See here . However, since you're using a TabActivity, why are you not calling getTabHost()
instead of getting the view manually?
这篇关于Tabhost NullPointerException异常时tabhost是不是在布局的唯一的事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!