4

タブレイアウトの例を試しました。また、例のいくつかのタイプミスを修正しました(そして、すべてのアクティビティをマニフェストに追加しました)。ただし、エミュレータで実行すると、最初の行にNullPointerExceptionが表示されます。

tabHost.addTab(spec);

もちろん、私の質問はそうです。この例外を引き起こす例の何が問題になっていますか?Eclipse Galileoを使用しており、ターゲットパッケージをAndroid1.5に設定しています。これまでのところ、Android開発サイトの他の例で他の問題は発生していません。

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>
4

12 に答える 12

9

マニフェストでこれを試してください:

<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> 
于 2010-04-22T13:08:23.537 に答える
2

ここに投稿されたすべての人に感謝したいと思います。私の問題を解決しました(ここにいる他のみんなと同じです)。これを機能させるのは非常にイライラしました。彼らは例をもっと単純化するべきでした。

必要な変更を要約してみます。

  1. Ngetha が言う 3 つのアクティビティ ラインを追加します。
  2. 3 つのアクティビティ クラスすべてを別のファイルに移動します。私のSongsActivity.javaファイルに使用した例(Eclipseエラーの提案付き)

    package com.example.HelloTabWidget;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    public class SongsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textview = new TextView(this);
        textview.setText("This is the Songs tab");
        setContentView(textview);
    }
    }
    
  3. res\drawable ディレクトリを作成し、そこにすべてのアイコンを配置する必要がありました。さらに、「ic_tab_songs.xml」などのアイコン用に 3 つの xml ファイルを作成しました。
于 2010-09-04T23:27:22.897 に答える
1

ねえ、次の手順を実行してください。問題が解決すると確信しています:-

クラスHelloTabWidget.javaを作成します。

package com.pericent;             //this is package name
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class HelloTabWidget extends TabActivity  {

private String TAG="HelloTabWidget";

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

  // Create an Intent to launch an Activity for the tab (to be reused)
  intent = new Intent().setClass(this,ArtistsActivity.class);
  Log.v(TAG,"---artist activity is called---");

  // 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);


  // Do the same for the other tabs
  intent = new Intent().setClass(this,AlbumsActivity.class);
  Log.v(TAG,"---album activity is called---");
  spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
  tabHost.addTab(spec);

  intent = new Intent().setClass(this, SongsActivity.class);
  Log.v(TAG,"---song activity is called---");
  spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
  tabHost.addTab(spec);

  }

}

2 番目のアクティビティを作成します: ArtistActivity.java

package com.pericent;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ArtistsActivity extends Activity {
 private String TAG="ArtistsActivity";
    /** Called when the activity is first created. */
    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textview=new TextView(this);
        textview.setText("This is Artist Activity");
        setContentView(textview);
        Log.v(TAG,"---in artist activity---");
    }
}

3 番目のアクティビティを作成します: AlbumsActivity.java

package com.pericent;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class AlbumsActivity extends Activity{
    private String TAG="AlbumsActivity";
    @Override
    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        TextView textview_album=new TextView(this);
        textview_album.setText("This is album activity");
        setContentView(textview_album);
        Log.v(TAG,"---in album activity---");
    }

}

4 番目のアクティビティを作成します: SongsActivity.java

package com.pericent;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SongsActivity extends Activity{
    private String TAG="SongsActivity";
    @Override
    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        TextView textview_song=new TextView(this);
        textview_song.setText("This is song activity");
        setContentView(textview_song);
        Log.v(TAG,"---in songs activity---");
    }

}

res/drawable に フォルダーを作成します。このフォルダーに 3 つの XML ファイルを作成します。これらのファイルのコードは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_tab_artists_grey"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_tab_artists_white" />
</selector>

上記の XML コードでは、2 つの画像を使用しています。以下は、同じフォルダー (res/drawable) に保存する必要がある画像です。

ここに画像の説明を入力 ここに画像の説明を入力

これは main.xmlです:

<?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" android:id="@+id/upper">
<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">
    <HorizontalScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none">
           <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
     </HorizontalScrollView>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>
</LinearLayout>

これは AdroidManifest です:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.pericent"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".HelloTabWidget" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >
          <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
       </activity>
       <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>
    </application>
</manifest> 

このすべての情報が役立つと思います。何か問題がある場合は、自由に私に質問してください。私はいつも誰かを助けることを嬉しく思います。

于 2011-09-19T05:58:49.350 に答える
1

私は同じ問題を抱えていました。昨日チュートリアルを開始しましたが、問題があったのはこれだけでした。

null ポインターは、最初に呼び出されたときにこの行でスローされます

tabHost.addTab(spec);

修正がマニフェスト XML にあったことが判明

<activity android:name=".MyTabActivity"
      android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>


<activity android:name=".DateActivity"  android:label="@string/app_name"></activity> 
<activity android:name=".RandomNumActivity"  android:label="@string/app_name"></activity> 

最後の 2 つのアクティビティは以前には存在しなかったため、失敗します。私はそれらを追加しました(上記のNgethaの提案に感謝します!)、それは完全に機能しました。チュートリアルの例自体では、追加する必要があるのは 3 つのアーティスト、アルバム、および曲のアクティビティです。

すべてのアクティビティをマニフェストにリストする必要があることを知ったと思いますが、T.

これが事実であるというのは本当ですか?すべての活動はマニフェストに含まれている必要がありますか?

于 2010-07-11T23:03:03.047 に答える
0

解決済み起動されたインテントから戻るときに、.addTab(spec)の後にnullPointerExceptionが発生していましたこのアクティビティへの最初のエントリでエラーは発生しませんでした。

私はそれを追加することで解決しました:

TabHost tabHost =(TabHost)getTabHost(); tabHost.setCurrentTab(0); //これによりnullPointerExceptionが停止しました........... tabHost.addTab(spec);

于 2010-10-30T11:31:34.103 に答える
0

このようなエラーが発生した場合は、Project > Clean を試して、自動生成されたファイルを再生成してください。

于 2010-06-24T23:11:24.377 に答える
0

私は Android のプログラミングを始めたばかりですが、この問題にも遭遇しました。私はそれにかなり不満を感じていたと言わざるを得ません。Ngetha が提案したことを実行することで解決しましたが、コードを少し編集する必要もありました。私が気づいたのは、どうやら Android はサブクラス化されたアクティビティを好まないということです。まったく。すべてをカプセル化することでコードをきれいにしようと思ったのですが、どうやらそうではないようです。クラスを別のファイルに移動する必要がありました。これが、私が抱えていたのと同じカプセル化の問題を抱えている他の新しいプログラマーに役立つことを願っています.

于 2010-06-01T04:14:11.173 に答える
0

同じ有線エラーが発生しました。

これを試して:

Eclipse を使用している場合は、エラーを削除し、いくつかのコードを変更して (アプリが再コンパイルされるように)、正常に動作するはずです。

于 2010-06-12T08:21:51.623 に答える
0

より良い仮定を立てることができるように、コードをもう少し投稿する必要があります。特に、tabHost Somewhere をインスタンス化するようにしてください。

于 2010-02-21T21:21:34.733 に答える
0

私もタブウィジェットのチュートリアルに問題がありました。

私はこのSOの投稿を読んで問題を解決することしかできませんでした。答えてくれてありがとう。

チュートリアルに戻ります.... Android 開発チームはドキュメントを改善できます。

実際、彼らはXMLアクティビティをマニフェストに追加するように私たちに指示していますが、それをうまく指定していないだけです..引用は次のとおりです。

これはレイアウト ファイルを使用しないことに注意してください。TextView を作成し、テキストを指定して、それをコンテンツとして設定するだけです。3 つのアクティビティごとにこれを複製し、対応するタグを Android マニフェスト ファイルに追加します。

于 2010-12-22T19:01:02.747 に答える
0

アクティビティを AndroidManifest.xml に入れましたか? 表示したいアクティビティについてアプリケーションに伝える必要があります。そうしないと、エラーが発生します。

たとえば、次の xml コードを <application> 要素内に配置できます。

    <activity android:name=".ArtistsActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

これが役立つことを願っています。

-スーリヤ・ウィジャヤ・マジッド

于 2010-03-31T09:21:56.110 に答える
0

これは私のために働いたものです:

この行を「artists」を使用するように変更しました tabHost.setCurrentTabByTag("artists");

そして、「。」を追加しました。TabAndroid(メインのアクティビティ名)の前に、Ngetha が提案した 3 つのアクティビティを追加しました。

</application>
于 2010-05-13T00:26:39.400 に答える