英文:
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<File> pdffiles = new ArrayList<>();
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(), "Page Refresh", Toast.LENGTH_SHORT).show();
mSwipeRefreshLayout.setRefreshing(false);
}
});
intiViews();
return view;
}
private void intiViews() {
folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
pdffiles = getPdfFiles(folder);
ArrayList<File> mypdf = getPdfFiles(Environment.getExternalStorageDirectory());
items = new String[mypdf.size()];
for (int i = 0; i < items.length; i++) {
items[i] = mypdf.get(i).getName().replace(".pdf", "");
}
pdfAdapter adapter = new pdfAdapter(getContext(), pdffiles, items);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
}
private ArrayList<File> getPdfFiles(File folder) {
ArrayList<File> arrayList = new ArrayList<>();
File[] file = folder.listFiles();
if (file != null) {
for (File singleFile : file) {
if (singleFile.isDirectory() && !singleFile.isHidden()) {
arrayList.addAll(getPdfFiles(singleFile));
} else {
if (singleFile.getName().endsWith(".pdf")) {
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(""));
}
return true;
}
@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 class
public class pdfAdapter extends RecyclerView.Adapter<pdfAdapter.MyViewHolder> {
private Context mContext;
private ArrayList<File> files;
String [] items;
public pdfAdapter(Context mContext, ArrayList<File> 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, "Edit", Toast.LENGTH_SHORT).show();
// break;
case R.id.delete:
// Toast.makeText(mContext, "Delete", 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, "Delete sucessfully", 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("position", 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<File> 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论