2011-03-28 62 views
0

我基于this tutorial的代码。为什么我会得到“R drawable can not draw”?

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

错误在于R绘制的ic_tabs_songs/albums/artists不能被绘制。 setContentView(R.layout.main);也无法绘制。我确定我在正确的文件夹中获得了图形,res/drawable/ldpi folder。我究竟做错了什么?

回答

0

您是否尝试过清洁项目?

项目 - >清洁

0

不幸的是,你提到的教程有一些错误。我也跟着你提到的教程,解决问题。 简而言之: 在'res'文件夹下创建一个名为drawable的文件夹。 enter image description here

和我的代码工作正常

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

又一个音符,你会发现,在本教程中,他们并没有在其中您必须自己添加清单文件中添加其他两项活动。

 <activity android:name="com.dbz.dbzatmactivities.Banks" 
     android:label="Banks"></activity> 
    <activity android:name="com.dbz.dbzatmactivities.Atms" 
     android:label="ATMs"></activity> 

这里,Banks和Atms是我的其他选项卡活动的名称。

相关问题