【发布时间】:2012-05-01 13:11:09
【问题描述】:
据此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>
【问题讨论】:
-
你把文件夹放在res文件夹下?你能给我们看看xml吗?
-
我把整个源代码加上图片请看一下
-
在 Eclipse 中使用
adb logcat、DDMS 或 DDMS 透视图检查 LogCat 并查看与“崩溃”相关的堆栈跟踪。 -
你不应该在 drawables 文件夹中有那些 .png 文件。可以发一下ic_tab_artists.xml的内容吗?
-
尝试在根元素处使用
<selector xmlns:android="http://schemas.android.com/apk/res/android">,而不是animation-list。