- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题是,当 MainActivity
恢复时,我的应用程序崩溃了。
错误是 IndexOutOfBoundsException
,不幸的是没有特定的行。错误显示 Index: 0, Size: 0
。
据我所知,这意味着我的 ArrayList
为空,我在初始化它之前调用它。但我在那个案子之后几天就开始搜索了。我什么也没找到。
所以首先是错误:
08-28 13:10:24.232 18741-18741/com.test.test.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.test.test, PID: 18741
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.firebase.ui.common.BaseObservableSnapshotArray.getSnapshot(BaseObservableSnapshotArray.java:70)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.firestore.FirestoreRecyclerAdapter.getItem(FirestoreRecyclerAdapter.java:83)
at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:125)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1858)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1191)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:876)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:895)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:730)
at android.view.View.layout(View.java:17689)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6274)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我的代码 fragment :
主要 Activity :
public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelper.RecyclerItemTouchHelperListener, NoteListAdapter.NotesAdapterListener {
private FirebaseAuth mFirebaseAuth;
public FirebaseUser mFirebaseUser;
private FirebaseFirestore db;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerViewLayoutManager;
public NoteListAdapter adapter;
private List<Note> notesList = new ArrayList<>();
private ListenerRegistration firestoreListener;
private RecyclerItemTouchHelper.RecyclerItemTouchHelperListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getInstance().getCurrentUser();
db = FirebaseFirestore.getInstance();
recyclerView = findViewById(R.id.rvNoteList);
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(true).build();
db.setFirestoreSettings(settings);
Query query = db.collection("users").document(firebase_user_uid).collection("notes");
FirestoreRecyclerOptions<Note> response = new FirestoreRecyclerOptions.Builder<Note>().setQuery(query, Note.class).build();
adapter = new NoteListAdapter(notesList, response, MainActivity.this, MainActivity.this);
recyclerViewLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new CustomRecyclerViewDivider(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(adapter);
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, MainActivity.this);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
firestoreListener = db.collection("users").document(firebase_user_uid).collection("notes")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("LOL", "Listen failed!", e);
return;
}
notesList.clear();
for (DocumentSnapshot doc : documentSnapshots) {
Note note = doc.toObject(Note.class);
note.setId(doc.getId());
notesList.add(note);
}
}
});
recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getApplicationContext(), recyclerView, new RecyclerViewTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
if (notesList != null || notesList.size() <= 0) {
Note note = notesList.get(position);
updateNote(note);
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
}
@Override
public void onStart() {
super.onStart();
if (adapter != null) {
adapter.startListening();
}
}
@Override
public void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
@Override
protected void onResume() {
super.onResume();
if (adapter != null) {
adapter.startListening();
}
}}
这是 NoteListAdapter:
public class NoteListAdapter extends FirestoreRecyclerAdapter<Note, NoteListAdapter.NoteViewHolder> implements Filterable{
private List<Note> notesList;
private List<Note> notesListFiltered;
private NotesAdapterListener listener;
private Context context;
public class NoteViewHolder extends RecyclerView.ViewHolder {
public TextView title, content;
public ImageView bg_note_image;
public RelativeLayout viewBackground, viewForeground;
public NoteViewHolder(View view) {
super(view);
title = view.findViewById(R.id.tvTitle);
content = view.findViewById(R.id.tvContent);
bg_note_image = view.findViewById(R.id.note_image_view);
viewBackground = view.findViewById(R.id.view_background);
viewForeground = view.findViewById(R.id.view_foreground);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onNoteSelected(notesListFiltered.get(getAdapterPosition()));
}
});
}
}
public NoteListAdapter(List<Note> notesList, FirestoreRecyclerOptions<Note> response, Context co, NotesAdapterListener listener) {
super(response);
context = co;
this.listener = listener;
this.notesList = notesList;
this.notesListFiltered = notesList;
}
@Override
protected void onBindViewHolder(NoteViewHolder holder, int position, Note model) {
if (notesListFiltered != null || notesList != null) {
final Note note = notesListFiltered.get(position);
holder.title.setText(note.getTitle());
holder.content.setText(note.getContent());
if (note.getNote_image_url() != null) {
Glide.with(context).load(note.getNote_image_url()).into(holder.bg_note_image);
holder.bg_note_image.setVisibility(View.VISIBLE);
}
}
}
@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_view, parent, false);
return new NoteViewHolder(view);
}
@Override
public void onError(FirebaseFirestoreException e) {
Log.e("LOL", e.getMessage());
}
@Override
public int getItemCount() {
return notesListFiltered.size();
}
public void removeItem(int position) {
notesList.remove(position);
// notify the item removed by position
// to perform recycler view delete animations
// NOTE: don't call notifyDataSetChanged()
notifyItemRemoved(position);
}
public void restoreItem(Note item, int position) {
notesList.add(position, item);
// notify item added by position
notifyItemInserted(position);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
notesListFiltered = notesList;
} else {
List<Note> filteredList = new ArrayList<>();
for (Note row : notesList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getTitle().toLowerCase().contains(charString.toLowerCase()) || row.getContent().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
notesListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = notesListFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
notesListFiltered = (ArrayList<Note>) filterResults.values;
notifyDataSetChanged();
}
};
}
public interface NotesAdapterListener {
void onNoteSelected(Note note);
}}
此外,我尝试通过此方法找到特定行,但没有成功:
@Override
protected void onResume() {
try {
super.onResume();
} catch (Exception e) {
e.printStackTrace();
Log.d("LOL", e.getMessage(), e);
}
}
我真的希望有人能解决我的问题,因为一周以来我真的不知道如何解决这个问题。
提前致谢。
最佳答案
检查下面的代码。希望对您有所帮助。您需要将所有 firebase
firestore
逻辑替换为 onResume()
方法。
public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelper.RecyclerItemTouchHelperListener, NoteListAdapter.NotesAdapterListener {
private FirebaseAuth mFirebaseAuth;
public FirebaseUser mFirebaseUser;
private FirebaseFirestore db;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerViewLayoutManager;
public NoteListAdapter adapter;
private List<Note> notesList = new ArrayList<>();
private ListenerRegistration firestoreListener;
private RecyclerItemTouchHelper.RecyclerItemTouchHelperListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rvNoteList);
}
@Override
public void onStart() {
super.onStart();
if (adapter != null) {
adapter.startListening();
}
}
@Override
public void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
@Override
protected void onResume() {
super.onResume();
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getInstance().getCurrentUser();
db = FirebaseFirestore.getInstance();
if (adapter != null) {
adapter.startListening();
}
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(true).build();
db.setFirestoreSettings(settings);
Query query = db.collection("users").document(firebase_user_uid).collection("notes");
FirestoreRecyclerOptions<Note> response = new FirestoreRecyclerOptions.Builder<Note>().setQuery(query, Note.class).build();
adapter = new NoteListAdapter(notesList, response, MainActivity.this, MainActivity.this);
recyclerViewLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new CustomRecyclerViewDivider(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(adapter);
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, MainActivity.this);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
firestoreListener = db.collection("users").document(firebase_user_uid).collection("notes")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("LOL", "Listen failed!", e);
return;
}
notesList.clear();
for (DocumentSnapshot doc : documentSnapshots) {
Note note = doc.toObject(Note.class);
note.setId(doc.getId());
notesList.add(note);
}
}
});
recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getApplicationContext(), recyclerView, new RecyclerViewTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
if (notesList != null || notesList.size() <= 0) {
Note note = notesList.get(position);
updateNote(note);
}
}
@Override
public void onLongClick(View view, int position) {
}
}))
}}
关于java - 仅在恢复 Activity 时获取 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056917/
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
((fn [coll] (letfn [(add-item [acc coll idx] (conj acc (nth coll idx))
我不是 Java 初学者,但也不是专家,这就是我发布此文章以寻求帮助/解释的原因。我在互联网上查找了很多地方,但没有找到我正在寻找的答案。 public class Driver { public s
我不断得到 线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException:MergeSorter.merge(MergeSorter.java:44)、
我做了一个小射击游戏..它工作正常但我也想实现如果火相交它们会消失。我有两个播放器子弹列表和计算机子弹列表......但是如果我有更多来自计算机或反向的子弹。这是我的循环 for (int
我试图从 JOptionPane 读取像 3+9-2*10/5 这样的数学表达式并得到它的结果——当然要考虑到操作顺序。我使用 String.split() 将字符串拆分为数字和操作数,并创建了一个寻
我在Grails中有以下代码: public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) { def li
这个问题已经有答案了: What is a debugger and how can it help me diagnose problems? (3 个回答) 已关闭 7 年前。 我有一组以下类型的
请看下面的代码 DatabaseHandler.java public List getDetails(String name) { List details = new Ar
我应该让蛇形矩形沿着屏幕的一侧移动。然而它保持在原位,当我运行它时,我还收到 IndexOutOfBoundsException 错误。我相信这与我的 Render.java 文件中的 for 循环有
ArrayList beds = new ArrayList(49); public Patient getPatient(int bedNumber) { if (beds.get(bedN
我有 10 张卡片的数组。在“回合”的过程中,我向数组添加一张卡片,然后从数组中删除一张卡片(并不总是相同的)。该代码在前 6-7 回合中效果很好,然后抛出 IndexOutofBoundsExcep
我正在尝试创建一个评分系统并获得最佳分数,但我的方法生成了 IndexOutOfBoundsException,但我找不到超出数组列表范围的内容,有人可以帮助我吗? 代码: public stati
为什么 int row = 1; // multiArray[col].length = 6 while(multiArray[col][row] > 1 && row 1) { 关于java -
我制作了一款游戏,我们控制一艘宇宙飞船,可以发射激光摧毁来自顶部的小行星。但在游戏过程中,每当它抛出 IndexOutOfBoundsException 时,我的游戏就会卡住。我不知道为什么会这样。请
我正在尝试实现合并排序,但我不断收到 IndexOutOfBoundsException 并且我无法找出原因。 我已经调试了我的程序,并且在此函数中我从未使用无效索引访问数组。引发异常的行也与抛出异常
我无法弄清楚为什么我的代码出现 IndexOutOfBoundsException。我想知道是否有人可以成为我的额外眼睛来帮助我发现错误。我正在手动尝试这个,但我认为在浏览代码时我遗漏了一些东西。 代
为什么我会收到此错误? Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.
我不知道这是否是一个简单的问题,但我只是看不出问题是什么。我现在从 Google Play 中的应用收到了三份关于 points.get(++i) 处的 IndexOutOfBoundsExcepti
如何执行以下内容 String p = "abcd"; System.out.print(p.substring(4)); 不会导致java.lang.IndexOutOfBoundsExceptio
我是一名优秀的程序员,十分优秀!