setOnItemClickListener在app chooser中不起作用

时间:2017-11-09 08:16:36

标签: android xml android-intent

我正在制作一个pdf查看器应用程序。它通过列表显示所有pdf文件,如果我单击一个,则显示文件内容。

当我单击列表中的文件时,该应用程序运行良好,但它会关闭并显示一条消息< Application has stopped>当我在“使用完整操作”屏幕中启动应用程序时。

我猜setOnItemClickListener()不适用于app选择器。我该如何解决这个问题?

MainActivity.java

ListView lv_pdf;
public static ArrayList<File> fileList = new ArrayList<File>();
PDFAdapter obj_adapter;
public static int REQUEST_PERMISSIONS = 1;
boolean boolean_permission;
File dir;

private void init() {
    lv_pdf = (ListView) findViewById(R.id.lv_pdf);
    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    fn_permission();

    lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getApplicationContext(), PdfActivity.class);
//PdfActivity.java file shows the content of a pdf file.

            intent.putExtra("position", i);
            startActivity(intent);

            Log.e("Position", i + "");
        }
    });
}

private void fn_permission() {
    if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
        if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE))) {

        }
        else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_PERMISSIONS);
        }
    }
    else {
        boolean_permission = true;

        getfile(dir);

        obj_adapter = new PDFAdapter(getApplicationContext(), fileList);
        lv_pdf.setAdapter(obj_adapter);
//PDFAdapter.java sends adapter to listview.
    }
}

的AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".PdfActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file" />
            <data android:scheme="content" />
            <data android:mimeType="application/pdf" />
        </intent-filter>
    </activity>

</application>

1 个答案:

答案 0 :(得分:0)

首先,您没有为列表视图提供任何适配器。

其次,您必须在适配器类中实现单击侦听器。

要解决这个问题,请为listview提供适当的适配器,然后在该适配器类中实现clicklistener方法。