Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Your content must have a ListView whose id attribute is 'android.R.id.list'" When changing from ListActivity to a view

I want to write an application that will a: Fill a list view with picture files from sdcard and b: display an image when choosing an option from the list.

However, it hangs with aforementioned exception. I can't see why...? Please, help!

Main activity:

public class ProjektJimmuActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navigateTo("/sdcard/");
    }

    public void navigateTo(String dir){
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,getListOfFiles(dir)));
        getListView().setTextFilterEnabled(true);
    }

    public String[] getListOfFiles(String dir){
        File file = new File(dir);
        String[] files = file.list(new Filter());
        return files;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
          Object item = l.getItemAtPosition(position);
          Toast.makeText(this,"Selection: "+ item.toString(), Toast.LENGTH_SHORT).show();
          setContentView(new PictureView(this,item.toString()));
    }

    private class Filter implements FilenameFilter{
        public boolean accept(File dir, String filename) {
            return (( filename.endsWith("jpg") || filename.endsWith("png") || filename.endsWith("gif") ) && !filename.startsWith("._") );
        } 
    }

}

Picture view:

public class PictureView extends View{

    private Bitmap bitmap;
    int width, height;
    private Canvas canvas;

    public PictureView(Context context, String pictureFile) {
        super(context);
        setFocusable(true);
        bitmap = BitmapFactory.decodeFile("/sdcard/"+pictureFile);
        height = bitmap.getHeight();
        width = bitmap.getWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xFFCCCCCC);
        canvas.drawBitmap(bitmap, null, null);
    }
}
like image 825
user905747 Avatar asked Mar 12 '12 22:03

user905747


1 Answers

Because you're extending ListActivity, you must have a ListView in your layout xml file, with the id, @android:id/list. extending ListActivity means that this activity, mainly is a list, but may have some components beside it. so, when you assume an activity is a list originally, you should have specify a list inside layout xml file of that acitivty, that is the main list. that list, should have the id, @android:id/list. so, simply set id field of the ListView of your activity, to @android:id/list.

like image 60
Keyhan Asghari Avatar answered Oct 22 '22 00:10

Keyhan Asghari