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 文件并放在不同的资源文件夹中吗?

一个 XML 文件res/drawable/就足够了,尽管您指向的各个可绘制资源StateListDrawable可能需要针对不同密度的不同版本。

于 2012-05-01T13:14:43.480 回答