简体   繁体   中英

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

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 .

In xml, in your listview, delete your previous listview I'd and set the I'd as follows this line :

android:Id="@android:id/list"

/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM