Android 无法将图标设置为tabspec

Android 无法将图标设置为tabspec,android,tabs,android-tabhost,Android,Tabs,Android Tabhost,我确信我已经设置好了图片和xml。我在android模拟器上运行了我的应用程序,它只是弹出一个窗口,显示应用程序停止运行。但删除图标部分后,包括res.getDrawable(R.drawable.tab\u icon1),res.getDrawable(R.drawable.tab\u icon2)和res。‌​getDrawable(R.drawable.tab\u图标3)该应用程序运行良好 public class ActionBar extends Activity implements

我确信我已经设置好了图片和xml。我在android模拟器上运行了我的应用程序,它只是弹出一个窗口,显示应用程序停止运行。但删除图标部分后,包括
res.getDrawable(R.drawable.tab\u icon1)
res.getDrawable(R.drawable.tab\u icon2)
res。‌​getDrawable(R.drawable.tab\u图标3)
该应用程序运行良好

public class ActionBar extends Activity implements OnClickListener {

TabHost th;
TextView showResult;
long start, stop;
Resources res;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_bar);
    th = (TabHost) findViewById(R.id.tabhost);
    th.setup();
    Button newTab = (Button) findViewById(R.id.bAddTab);
    Button start = (Button) findViewById(R.id.bStart);
    Button stop = (Button) findViewById(R.id.bStop);
    showResult = (TextView) findViewById(R.id.textView1);
    newTab.setOnClickListener(this);
    start.setOnClickListener(this);
    stop.setOnClickListener(this);
    TabSpec specs = th.newTabSpec("tab1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("Stop Watch", res.getDrawable(R.drawable.tab_icon1));
    th.addTab(specs);
    specs = th.newTabSpec("tab2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("Add Tab", res.getDrawable(R.drawable.tab_icon2));
    th.addTab(specs);
    specs = th.newTabSpec("tab3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("Blank", res.getDrawable(R.drawable.tab_icon3));
    res = getResources();
    th.addTab(specs);
}

我看到的一件简单的事情是,您应该定义
res=getResources()
在所有三个位置使用
res
变量之前。现在您正在使用它,在定义它之前,这可能会抛出一个Exceptionoon。做一些类似于:

//....
res = getResources();
specs.setContent(R.id.tab1);
specs.setIndicator("Stop Watch", res.getDrawable(R.drawable.tab_icon1));
th.addTab(specs);
specs = th.newTabSpec("tab2");
specs.setContent(R.id.tab2);
specs.setIndicator("Add Tab", res.getDrawable(R.drawable.tab_icon2));
th.addTab(specs);
specs = th.newTabSpec("tab3");
specs.setContent(R.id.tab3);
specs.setIndicator("Blank", res.getDrawable(R.drawable.tab_icon3));
//....
如果您需要进一步的帮助,也请发布LogCat信息。
希望这有帮助

@SimonH当我运行应用程序时,应用程序停止了,但是删除了图标部分,包括res.getDrawable(R.drawable.tab_icon1)、res.getDrawable(R.drawable.tab_icon2)、res.getDrawable(R.drawable.tab_icon3),应用程序运行良好。我在android仿真器上运行了应用程序,它只是弹出一个窗口,显示应用程序停止了运行。谢谢你,它工作得很好!!还有一个问题,图标显示在我的2.2版上,但在emulator 4.2版上,它只有文本,为什么?@Xiu Tiger Yi:它在emulator for 4.2 android上会出现什么错误?你能帮我做记录吗?