我正在尝试从drawable文件夹访问图像文件;直接在我的Java类中。我终于达到了我的应用程序运行的程度,但它不会显示图像。我的目标是让图像显示在屏幕顶部;字符串“viewSelection”上方。这是我的代码。谁能告诉我我错过了什么?
public class Menu extends ListActivity {
String viewSelection[] = { "FirstView"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, viewSelection));
ImageView imgView=(ImageView) findViewById(R.id.imgView);
Drawable drawable = getResources().getDrawable(R.drawable.img);
imgView.setImageDrawable(drawable);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String thelist = viewSelection[position];
try{
@SuppressWarnings("rawtypes")
Class ourClass = Class.forName("com.java.notcoffee." + thelist);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
我修改了OnCreate方法来修复它:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, viewSelection));
ImageView imgView=(ImageView) findViewById(R.id.imgView);
imgView.setImageResource(R.drawable.img);
}