3

このhttp://developer.android.com/guide/topics/resources/drawable-resource.html#StateListによると

開発者ページには、リソースの状態を示すアイコンを保持する XML ファイルを作成すると書かれています。しかし、デフォルトでは新しいプロジェクトが作成され、この Drawable フォルダーは存在しませんが、異なる解像度の画像を示す 4 つの diff フォルダーが作成されます。

Drawableという名前のフォルダーを強制的に作成し、XMLファイルを配置してアプリケーションで使用しましたが、残念ながらアプリケーションはリソースがNULLであるという例外をスローします。この問題を克服するにはどうすればよいですか?

XML ファイルをどこに置く必要がありますか?個々の XML ファイルを作成し、別のリソース フォルダーに配置する必要がありますか? 親切に私にアドバイスしてください

フォルダ構成イメージはこちら。

ここに画像の説明を入力

ic_tab_artist を使用すると、アプリがクラッシュします。ここに完全なソースがあります

package simple.tab.proj;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;

public class SimpleTabActivity extends TabActivity  {
    /** Called when the activity is first created. */
    @Override
    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);

        // 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, ArtistsActivity.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

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

        tabHost.setCurrentTab(2);

    }



    public static class ArtistsActivity extends Activity {
        /** 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 the Artists tab");
            setContentView(textview);

            //setContentView(R.layout.main);
        }
    }
}

これは私の 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>

これが私のic_artist_tab.xmlです

<?xml version="1.0" encoding="utf-8"?>
<animation-list 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" />
</animation-list>
4

1 に答える 1

1

Drawableという名前のフォルダーを強制的に作成し、XMLファイルを配置してアプリケーションで使用しましたが、残念ながらアプリケーションはリソースがNULLであるという例外をスローします。この問題を克服するにはどうすればよいですか?

Android では大文字と小文字が区別されます。res/drawable/ディレクトリはではなくである必要がありDrawableます。

個々の XML ファイルを作成し、別のリソース フォルダーに配置する必要がありますか?

1 つの XML ファイルでres/drawable/十分ですが、あなたが指している個々のドローアブル リソースにはStateListDrawable、異なる密度に対して異なるバージョンが必要になる場合があります。

于 2012-05-01T13:14:43.480 に答える