Why am I getting "R drawable cannot be drawn"?
I'm basing my code off of this tutorial.
package com.Tabs.org;
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 {
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, AlbumsActivity.class);
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);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
The error is that the R drawable ic_tabs_songs/albums/artists cannot be drawn. setContentView(R.layout.main);
also cannot be drawn. I'm sure I got the graphics in the right folder, res/drawable/ldpi folder
. What am I doing wrong?
Have you tried cleaning the project?
Project -> Clean
unfortunately, the tutorial you mentioned has some mistakes. I also followed your mentioned tutorial and solve the problems. In short: Create a folder named drawable just under 'res' folder.
and my code works fine
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Banks.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("banks").setIndicator("Banks",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
Just another note, you will find that, in the tutorial, they didn't add the other two activities in the manifest file which you must add by yourself.
<activity android:name="com.dbz.dbzatmactivities.Banks"
android:label="Banks"></activity>
<activity android:name="com.dbz.dbzatmactivities.Atms"
android:label="ATMs"></activity>
Here, Banks and Atms are the name of my other tab activities.
精彩评论