问题运行Android HelloTabWidget的例子 - 在addTab NullPointerException异常() [英] Problem running android HelloTabWidget example - NullPointerException on addTab()

查看:91
本文介绍了问题运行Android HelloTabWidget的例子 - 在addTab NullPointerException异常()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过了标签布局例如和我还固定了几个错别字的例子(添加了所有活动的清单)。然而,当我在模拟器上运行它,我得到的第一行写着

一个NullPointerException异常
  

tabHost.addTab(规范);

所以我的问题,当然是。有什么不对,将导致该异常的例子吗?我使用的Eclipse伽利略和设定目标包为Android 1.5的。到目前为止,我已经没有其他问题上的Andr​​oid开发人员网站上的其他例子。

 包com.example.hellotabwidget;

进口android.app.TabActivity;
进口android.content.Intent;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.widget.TabHost;

公共类HelloTabWidget扩展TabActivity {
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState)抛出的RuntimeException {
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    资源RES = getResources(); //资源对象获取可绘
    TabHost tabHost = getTabHost(); //活动TabHost
    TabHost.TabSpec规范; //可重复使用的则tabspec为每个标签
    意向意图; //可重复使用意向为每个标签

    //创建一个Intent来启动一个活动的标签(重复使用)
    //最后的上下文语境= getApplicationContext();
    意图=新的意图()setClass(这一点,ArtistsActivity.class)。

    //初始化则tabspec为每个标签,并把它添加到TabHost
    规格= tabHost.newTabSpec(艺术家)。setIndicator(艺术家,
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(意向);
    tabHost.addTab(规范); //运行此行之后******** NullPointerException异常

    //执行相同的其他标签
    意图=新的意图()setClass(这一点,AlbumsActivity.class)。
    规格= tabHost.newTabSpec(相册)。setIndicator(相册,
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(意向);
    tabHost.addTab(规范);

    意图=新的意图()setClass(这一点,SongsActivity.class)。
    规格= tabHost.newTabSpec(诗经)。setIndicator(诗经,
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(意向);
    tabHost.addTab(规范);

    tabHost.setCurrentTabByTag(艺术家);
}
}
 

main.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人: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>
 

解决方案

尝试在你的清单:

 <活动机器人:名称=机器人AlbumsActivity。:标签=@字符串/ APP_NAME>< /活性GT;
    <活动机器人:ArtistsActivity名称=机器人:标签=@字符串/ APP_NAME>< /活性GT;
    <活动机器人:名称=机器人SongsActivity。:标签=@字符串/ APP_NAME>< /活性GT;
 

I've tried the Tab Layout example, and I've also fixed the few typos in the example (and added all the activities to the manifest). However, when I run it on the emulator I get a NullPointerException on the first line that says

tabHost.addTab(spec);

So my question, of course, is. What is wrong with the example that would cause this exception? I'm using Eclipse Galileo and set the target package as Android 1.5. So far I've had no other problems with the other examples on the android dev site.

package com.example.hellotabwidget;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

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

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

    // Create an Intent to launch an Activity for the tab (to be reused)
    //final Context context = getApplicationContext();
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(intent);
    tabHost.addTab(spec); //******** NullPointerException after running this line

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(intent);
    tabHost.addTab(spec);

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

    tabHost.setCurrentTabByTag("artists");
}
}

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="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>

解决方案

Try this in your manifest:

<activity android:name=".AlbumsActivity"  android:label="@string/app_name"></activity> 
    <activity android:name=".ArtistsActivity"  android:label="@string/app_name"></activity> 
    <activity android:name=".SongsActivity"  android:label="@string/app_name"></activity> 

这篇关于问题运行Android HelloTabWidget的例子 - 在addTab NullPointerException异常()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆