I am trying to access an image file from the drawable folder; directly in my Java class. I have finally reached the point where my app will run, however it will not display the image. My Goal is to have the image display at the top of the screen; above the string “viewSelection”. This is my code. Can anyone tell me what I am missing?
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();
}
}
}
I modified the OnCreate method to fix it :