我試圖使用一個片段和一個回圈器視圖配接器來顯示一個影像視圖串列。
我每次都會得到這個例外
行程:com.example.plasmavietiles, PID: 4860
java.lang.NullPointerException。試圖在一個'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()'上呼叫虛擬方法null物件參考。
at androidx.recyclerview.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:4311)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4045)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:17637)
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:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:148)
at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43)
at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1892)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
at android.view.View.layout(View.java:17637)
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:17637)
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:17637)
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:726)
at android.view.View.layout(View.java:17637)
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:6337)
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:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
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)
下面是我的代碼:
QuestionnaireActivitypublic class QuestionnaireActivity extends Activity implements View. OnClickListener, View.OnTouchListener {
private ArrayList<Integer> questionList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
setContentView(R.layout.activity_questionnaire)。
/*Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
prepareView()。
FragmentManager fragmentManager = getFragmentManager()。
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()。
QuestionFragment question = new QuestionFragment()。
question.setQuestionArray(questionList)。
fragmentTransaction.replace(R.id.questionnaireFragment,question)。
fragmentTransaction.commit()。
}
@Override
protected void attachBaseContext(Context base)
{
super.attachBaseContext(LocalHelper.wrap(base) )。
}
@SuppressLint("ClickableViewAccessibility")
private void prepareView()
{
this.questionList = new ArrayList<>()。
Random r = new Random()。
int tmp;
for(int i =0; i<5;){
tmp = r.nextInt((20 - 1) 1) 1;
if(!this.questionList.contains(tmp))
{
this.questionList.add(tmp)。
i ;
}
}
}
}
問題片段類:
public class QuestionFragment extends Fragment {
// TODO:自定義引數引數名稱。
private static final String ARG_COLUMN_COUNT = "column-count" 。
// TODO:自定義引數。
private int mColumnCount = 1;
List<Integer> questionArray = new ArrayList<>()。
/***。
* 必備的空建構式,用于片段管理器實體化的
* 片段(例如,在螢屏方向改變時)。
*/
public QuestionFragment() {
}
public void setQuestionArray(List< Integer> questionArray) {
this.questionArray = questionArray;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
/* if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT)。
}*/
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_question_list, container, false) 。
//設定配接器。
if (view instanceof RecyclerView) {
Context context = view.getContext()。
RecyclerView recyclerView = (RecyclerView)視圖。
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context)) 。
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)) 。
}
recyclerView.setAdapter(new MyQuestionRecyclerViewAdapter(questionArray))。
}
return view。
}
我的回收器視圖配接器類:
public class MyQuestionRecyclerViewAdapter extends RecyclerView. 配接器<MyQuestionRecyclerViewAdapter.ViewHolder> {
private final List<Integer> mValues;
public MyQuestionRecyclerViewAdapter(List<Integer> items) {
mValues = items;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()
.inflate(R.layout.fragment_question, parent, false)。
return new ViewHolder(view)。
}
public void onBindViewHolder(final ViewHolder holder, int position) {
// holder.mItem = mValues.get(position);
// holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setImageResource(R.drawable.facebook_icon)。
}
public int getItemCount() {
return mValues.size()。
}
public class ViewHolder extends RecyclerView.ViewHolder{
public final ImageView mContentView。
public ViewHolder(View view) {
super(view)。
/mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (ImageView) view.findViewById(R.id.imageView10)。
}
}
}
uj5u.com熱心網友回復:
也許你想交換這些布局
。之前的碎片類
View view = inflater. inflate(R.layout.fragment_question_list, container, false)。
配接器類之前
View view = LayoutInflater.from(parent.getContext()
.inflate(R.layout.fragment_question, parent, false)。
片段類之后
View view = inflater。 inflate(R.layout.fragment_question, container, false) 。
配接器類之后
View view = LayoutInflater.from(parent.getContext()
.inflate(R.layout.fragment_question_l8st, parent, false)。
uj5u.com熱心網友回復:
不要檢查視圖是否是recyclerview的實體,而是直接通過id找到recyclerview。
RecyclerView recycler_view = view.findViewById(R.id.cyclerViewId)。
Context context = view.getContext()。
RecyclerView recyclerView = (RecyclerView)視圖。
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context)) 。
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)) 。
}
recyclerView.setAdapter(new MyQuestionRecyclerViewAdapter(questionArray))。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318394.html
標籤:
上一篇:根據主題設定文本顏色-android/kotlin錯誤
下一篇:在默認介面方法中回傳通用型別的類
