1

我想在 Android 列表视图中打开一个选定的项目。如果是图像,我想显示它,如果是文本文件,那么我想打开它,但是我的代码不起作用,这里是 oncreate 方法

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView lv;
        ArrayList<String> FilesInFolder = GetFiles("/sdcard/DCIM/Camera/", true);
        lv = (ListView) findViewById(R.id.filelist);

        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, FilesInFolder));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // Clicking on items
                String name_of_file =       lv.getItemAtPosition(position).toString();
                Intent myIntent = new Intent(v.getContext(), MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("/sdcard/DCIM/Camera/", name_of_file);
                myIntent.putExtras(bundle);
                try {
                    startActivity(myIntent);
                    finish();
                } catch (android.content.ActivityNotFoundException e) {
                }
            }
        });

    }
4

1 回答 1

0

也许您的 startIntent 触发了 ActivityNotFoundException 异常......

您是否在 AndroidManifest.xml 文件中添加了 MainActivity?

于 2013-03-24T01:34:54.357 回答