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

I want to write an application that will: fill in the list view with image files from the SD card and b: display the image when you select an option from the list.

However, it hangs with the above exception. I do not understand why...? Please, help!

Primary 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("._") ); } } } 

Image Type:

 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); } } 
+6
source share
2 answers

As you extend ListActivity , you must have a ListView in your layout XML file with the identifier @android:id/list . the ListActivity extension means that this activity is basically a list, but may have some components near it. therefore, when you assume that the action is a list initially, you must specify the list inside the XML file of this acitivty layout, that is, the main list. this list must have an identifier, @android:id/list . so just set the id field of the ListView your activity to @android:id/list .

+12
source

In xml, in your list, delete the previous list, and I would set the following line:

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

/ ">

-2
source

Source: https://habr.com/ru/post/910546/


All Articles