Attempt to invoke virtual method 'void pdfreader.pdfconverter.pdfreaderapp.pdfAdapter.updatePdfFile(java.util.ArrayList)' on a null object reference

huangapple go评论71阅读模式
英文:

Attempt to invoke virtual method 'void pdfreader.pdfconverter.pdfreaderapp.pdfAdapter.updatePdfFile(java.util.ArrayList)' on a null object reference

问题

在我在searchView中输入文本以搜索项目时,我的应用程序崩溃了。我有一个片段和一个适配器。接下来我该怎么做?有人能帮帮我。

这是我收到的错误信息-
尝试调用空对象引用上的'void pdfreader.pdfconverter.pdfreaderapp.pdfAdapter.updatePdfFile(java.util.ArrayList)'方法

以下是片段的代码

public class Pdf_Reader_fragments extends Fragment implements SearchView.OnQueryTextListener {

    // ... 其他代码 ...

    @Override
    public boolean onQueryTextChange(String newText) {

        String userInput = newText.toLowerCase();
        ArrayList<File> myfile = new ArrayList<>();

        for (File pdf : pdffiles){
            if (pdf.getName().toLowerCase().contains(userInput)){
                myfile.add(pdf);
            }
        }

        adapter.updatePdfFile(myfile);

        return true;
    }
}

pdfAdapter 类

public class pdfAdapter extends RecyclerView.Adapter<pdfAdapter.MyViewHolder> {

    // ... 其他代码 ...

    void updatePdfFile(ArrayList<File> fileArrayList){
        files.clear();
        files.addAll(fileArrayList);
        notifyDataSetChanged();
    }
}

希望这些内容对您有所帮助。如果您需要更多帮助,请随时问我。

英文:

When I am typing text in searchView for search items, my app getting crashed. I have a fragment and adapter. What I do next? Someone help me.

This is the error I got-
Attempt to invoke virtual method 'void pdfreader.pdfconverter.pdfreaderapp.pdfAdapter.updatePdfFile(java.util.ArrayList)' on a null object reference

here is fragment code

public class Pdf_Reader_fragments extends Fragment implements SearchView.OnQueryTextListener {

    public static ArrayList&lt;File&gt; pdffiles = new ArrayList&lt;&gt;();

    RecyclerView recyclerView;

    File folder;
    String[] items;
    static pdfAdapter adapter;

    int number = 0;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    public Pdf_Reader_fragments() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pdf__reader, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recylerView);

        mSwipeRefreshLayout = view.findViewById(R.id.swip);
        setHasOptionsMenu(true);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                try {
                    intiViews();
                } catch (Exception e) {

                }

                Toast.makeText(getContext(), &quot;Page Refresh&quot;, Toast.LENGTH_SHORT).show();
                mSwipeRefreshLayout.setRefreshing(false);


            }
        });

        intiViews();
        return view;
    }


    private void intiViews() {
        folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        pdffiles = getPdfFiles(folder);

        ArrayList&lt;File&gt; mypdf = getPdfFiles(Environment.getExternalStorageDirectory());
        items = new String[mypdf.size()];
        for (int i = 0; i &lt; items.length; i++) {
            items[i] = mypdf.get(i).getName().replace(&quot;.pdf&quot;, &quot;&quot;);
        }
        pdfAdapter adapter = new pdfAdapter(getContext(), pdffiles, items);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));

    }

    private ArrayList&lt;File&gt; getPdfFiles(File folder) {
        ArrayList&lt;File&gt; arrayList = new ArrayList&lt;&gt;();
        File[] file = folder.listFiles();

        if (file != null) {
            for (File singleFile : file) {
                if (singleFile.isDirectory() &amp;&amp; !singleFile.isHidden()) {
                    arrayList.addAll(getPdfFiles(singleFile));
                } else {
                    if (singleFile.getName().endsWith(&quot;.pdf&quot;)) {
                        arrayList.add(singleFile);
                    }
                }
            }
        }
        return arrayList;
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();

        inflater.inflate(R.menu.search, menu);
        MenuItem item = menu.findItem(R.id.search_button);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);

        SearchView searchView = (SearchView) item.getActionView();
        searchView.setOnQueryTextListener(this);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        if (adapter != null) {

            query = String.valueOf(query.equals(&quot;&quot;));
        }
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        String userInput = newText.toLowerCase();
        ArrayList&lt;File&gt; myfile = new ArrayList&lt;&gt;();

        for (File pdf : pdffiles){
            if (pdf.getName().toLowerCase().contains(userInput)){
                myfile.add(pdf);
            }

            adapter.updatePdfFile(myfile);
        }

          return true;
    }


    }

pdfAdapter class

public class pdfAdapter extends RecyclerView.Adapter&lt;pdfAdapter.MyViewHolder&gt; {
    private Context mContext;
    private ArrayList&lt;File&gt; files;
   

    String [] items;


    public pdfAdapter(Context mContext, ArrayList&lt;File&gt; files, String[] items) {
        this.mContext = mContext;
        this.files = files;
        this.items = items;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.pdf_items, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        holder.file_name.setText(items[position]);

        holder.popUpMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                PopupMenu popupMenu = new PopupMenu(mContext, v);
                popupMenu.getMenuInflater().inflate(R.menu.menu, popupMenu.getMenu());
                popupMenu.show();
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
//                            case R.id.edit:
//                                Toast.makeText(mContext, &quot;Edit&quot;, Toast.LENGTH_SHORT).show();
//                                break;
                            case R.id.delete:

                               // Toast.makeText(mContext, &quot;Delete&quot;, Toast.LENGTH_SHORT).show();
                                deleteFile(position, v);

                        }
                        return true;
                    }
                   public void deleteFile (final int position, View v) {

                  String uri = files.get(position).getAbsolutePath();
                          final File file = new File (uri);
                       boolean deleted = file.delete();
                       if (deleted) {
                           files.remove(position);
                           notifyItemRemoved(position);
                           notifyItemRangeChanged(position, files.size());
                           Toast.makeText(mContext, &quot;Delete sucessfully&quot;, Toast.LENGTH_SHORT).show();
                       }
                   }
                });


            }
        });
        holder.pdf_Layouts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PdfViewActivity.class);
                intent.putExtra(&quot;position&quot;, position);
                mContext.startActivity(intent);
            }});

    }

    @Override
    public int getItemCount() {
        return files.size();
    }






    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView file_name;
        ImageView file_icon,popUpMenu;
        RelativeLayout pdf_Layouts;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            file_name = itemView.findViewById(R.id.pdf_item_name);
            file_icon = itemView.findViewById(R.id.img_pdf);
            pdf_Layouts = itemView.findViewById(R.id.pdf_items_h);
            popUpMenu  = itemView.findViewById(R.id.more);

        }
    }

    void updatePdfFile(ArrayList&lt;File&gt; fileArrayList){
//       
        files.clear();
        files.addAll(fileArrayList);
        notifyDataSetChanged();
    }

}

答案1

得分: 1

你没有将适配器分配给你的类变量 static pdfAdapter adapter;

private void intiViews() 函数中将:

pdfAdapter adapter = new pdfAdapter(getContext(), pdffiles, items); 替换为

adapter = new pdfAdapter(getContext(), pdffiles, items);

英文:

You are not assigning adapter to your class variable static pdfAdapter adapter;

In private void intiViews() function change:

pdfAdapter adapter = new pdfAdapter(getContext(), pdffiles, items); to

adapter = new pdfAdapter(getContext(), pdffiles, items);

huangapple
  • 本文由 发表于 2020年8月28日 13:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63628213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定