Android开发中的NullPointerException

mdfafbf1  于 2023-02-10  发布在  Android
关注(0)|答案(3)|浏览(121)

'在com.示例.pdfreader.主活动.查找Pdf(主活动. java:63)在com.示例.pdfreader.主活动.查找Pdf(主活动. java:67)在com.示例.pdfreader.主活动.查找Pdf(主活动. java:67)在com.示例.pdfreader.主活动.显示Pdf(主活动. java:84)在com.示例. pdfreader.主活动$1.已授予权限(主活动. java:44)
我正在特灵构建PdfReader应用程序,并在ArrayList findpdf()中;方法,我得到了一些异常,它会导致应用程序中的bug,所以它的消失被crahshed '

package com.example.pdfreader;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnPdfSelectListner{

    private MainAdapter adapter;
    private List<File> pdfList;
    private RecyclerView recyclerView;

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

    }

    private void runtimePermission(){
        Dexter.withContext(MainActivity.this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                       displayPdf();
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
                        Toast.makeText(MainActivity.this, "Permission Requried", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
                        permissionToken.continuePermissionRequest();
                    }
                }).check();
    }

    public ArrayList<File> findPdf (File file)
    {
        ArrayList<File> arrayList = new ArrayList<> ();
        File[] files = file.listFiles();
            for (File singleFile :files)
            {
                if (singleFile.isDirectory () && !singleFile.isHidden())
                {
                    arrayList.addAll(findPdf(singleFile));
                }
                else{
                    if(singleFile.getName().endsWith(".pdf")) {
                        arrayList.add(singleFile);
                    }
                }
            }
        return  arrayList;
    }

    public void displayPdf()
    {
        recyclerView = findViewById(R.id.rv);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        pdfList = new ArrayList<> ();
        pdfList.addAll(findPdf (Environment.getExternalStorageDirectory()));
        adapter = new MainAdapter(this, pdfList,this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onPdfSelected(File file) {
            startActivity(new Intent(MainActivity.this,PdfActivity.class)
                    .putExtra("paht",file.getAbsolutePath()));

    }
}

Here, is the all code of the MainActivity.java and although on calling the displayPdf(); method in runtimePermission(); is ok or shall we need to modify this functions.
neskvpey

neskvpey1#

if (singleFile==null){
         findPdf(null);
                }       
i had add this in for each loop of findPdf but it won't work until
t40tm48m

t40tm48m2#

下面是获取外部目录中所有pdf文件的方法。根据您的使用情况进行定制。

public ArrayList<String> getAllPdf() {
        Uri uri = MediaStore.Files.getContentUri("external");
        String[] projection = {MediaStore.Files.FileColumns.DISPLAY_NAME, MediaStore.Files.FileColumns.DATA};
        String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ?";
        String[] selectionArgs = {"application/pdf"};
        String sortOrder = MediaStore.Files.FileColumns.DISPLAY_NAME + " ASC";

        Cursor cursor = requireActivity().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);

        ArrayList<String> pdfFiles = new ArrayList<>();

        if (cursor != null) {
            while (cursor.moveToNext()) {
                String fileName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME));
                String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));

                pdfFiles.add(fileName + " (" + filePath + ")");
            }
            cursor.close();
        }
        return pdfFiles;
    }
9njqaruj

9njqaruj3#

public ArrayList<File> findPdf(File file) {
    ArrayList<File> pdfFiles = new ArrayList<>();
    try {
        File[] files = file.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    pdfFiles.addAll(findPdf(f));
                } else if (f.getName().endsWith(".pdf")) {
                    pdfFiles.add(f);
                }
            }
        }
    } catch (NullPointerException e) {
        Log.e("findPdf", "NullPointerException: " + e.getMessage());
    }
    return pdfFiles;
}

Even I tried this to catch nullpointerexception but it won't work again but now the app not gone be crashed.

相关问题