Java Android:RecycleServiceAdapter中的GetApplication
我想在onClickListener方法中从一个Recyclerview中膨胀一个Recyclerview 为此,我知道我必须从相应的活动中获取上下文,以设置LinearLayoutManager 问题: 因为我正在使用一个Recyclerview in片段来尝试创建另一个Recyclerview,所以我不知道如何获得正确的上下文 我的尝试:Java Android:RecycleServiceAdapter中的GetApplication,java,android,android-recyclerview,android-context,recyclerview-layout,Java,Android,Android Recyclerview,Android Context,Recyclerview Layout,我想在onClickListener方法中从一个Recyclerview中膨胀一个Recyclerview 为此,我知道我必须从相应的活动中获取上下文,以设置LinearLayoutManager 问题: 因为我正在使用一个Recyclerview in片段来尝试创建另一个Recyclerview,所以我不知道如何获得正确的上下文 我的尝试: 2018-11-11 17:46:15.589 21538-21538/com.example.chris.projectartifact E/Andro
2018-11-11 17:46:15.589 21538-21538/com.example.chris.projectartifact E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chris.projectartifact, PID: 21538
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.chris.projectartifact.b_deckbuilderTap.deckbuilder_RViewAdapter_Card$3.onClick(deckbuilder_RViewAdapter_Card.java:116)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
- 在我的构造函数中使用FragmentActivity mContext,稍后尝试通过“mContext.getApplicationContext()”获取上下文。(从另一篇文章中获取FragmentActivity,事先使用了context mContext)
public deckbuilder_RViewAdapter_Card(FragmentActivity mContext, List<Cards> mData) {
this.mData = mData;
this.mContext = mContext;
}
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
fab_deckbuilder_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Decklist> listCards = new ArrayList<>();
listCards.add(new Decklist( ... );
LinearLayoutManager layoutManagerCards = new LinearLayoutManager(mContext.getApplicationContext(), LinearLayoutManager.VERTICAL, false);
rvList.setLayoutManager(layoutManagerCards);
deckbuilder_RViewAdapter_List addCardAdapter = new deckbuilder_RViewAdapter_List(mContext, listCards);
rvList.setAdapter(addCardAdapter);
}
});
}
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView ivCardImage;
TextView tvCardName;
public ViewHolder(@NonNull View introListView) {
super(introListView);
fab_deckbuilder_add = introListView.findViewById(R.id.fab_deckbuilder_add);
fab_deckbuilder_remove = introListView.findViewById(R.id.fab_deckbuilder_remove);
rvList = introListView.findViewById(R.id.rv_deckbuilder_list);
}
}
private List<Decklist> mDecklist;
private Context mContext;
public deckbuilder_RViewAdapter_List (Context mContext, List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_cardview_deckbuilder_list, viewGroup, false);
return new deckbuilder_RViewAdapter_List.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
viewHolder.ivCard.setImageResource(mDecklist.get(position).getCardImage());
viewHolder.ivType.setImageResource(mDecklist.get(position).getTypeImage());
viewHolder.tvName.setText(mDecklist.get(position).getName());
viewHolder.tvCost.setText(mDecklist.get(position).getCost());
viewHolder.tvNumber.setText(mDecklist.get(position).getNumber());
}
public int getItemCount() {
return mDecklist.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView ivCard, ivType;
TextView tvName, tvCost, tvNumber;
public ViewHolder(@NonNull View itemView) {
super(itemView);
ivCard = itemView.findViewById(R.id.ivDecklistCardImage);
ivType = itemView.findViewById(R.id.ivDecklistTypeImage);
tvName = itemView.findViewById(R.id.tvDecklistName);
tvCost = itemView.findViewById(R.id.tvDecklistCost);
tvNumber = itemView.findViewById(R.id.tvDecklistNumber);
}
}
请求的编辑:
2018-11-11 17:46:15.589 21538-21538/com.example.chris.projectartifact E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chris.projectartifact, PID: 21538
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.chris.projectartifact.b_deckbuilderTap.deckbuilder_RViewAdapter_Card$3.onClick(deckbuilder_RViewAdapter_Card.java:116)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
布局卡片视图甲板列表:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardView_deckbuilder_list_id"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview= "http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="20dp"
android:layout_marginTop="5dp"
cardview:cardCornerRadius="5dp"
android:layout_marginStart="5dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/axe"
cardview:layout_constraintEnd_toStartOf="@id/gl_v_db_list"
cardview:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/ivDecklistCardImage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:scrollY="-7dp"
android:src="@drawable/thunder_gods_wrath"
cardview:layout_constraintEnd_toStartOf="@id/gl_v_db_list"
cardview:layout_constraintStart_toStartOf="parent" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/ivDecklistTypeImage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/introspells"
cardview:layout_constraintEnd_toStartOf="@id/gl_v_db_list_secound"
cardview:layout_constraintStart_toStartOf="@id/gl_v_db_list" />
<TextView
android:id="@+id/tvDecklistCost"
android:layout_width="0dp"
android:layout_height="match_parent"
cardview:layout_constraintStart_toStartOf="@id/gl_v_db_list_secound"
android:text="20"
android:autoSizeMaxTextSize="16sp"
android:autoSizeMinTextSize="8sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:textColor="#ffffff"
cardview:layout_constraintEnd_toEndOf="@id/gl_v_db_list_third"/>
<TextView
android:id="@+id/tvDecklistName"
android:layout_width="0dp"
android:layout_height="match_parent"
cardview:layout_constraintStart_toStartOf="@id/gl_v_db_list_third"
android:text="Thunder"
android:autoSizeMaxTextSize="20sp"
android:autoSizeMinTextSize="8sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:textColor="#ffffff"
cardview:layout_constraintEnd_toEndOf="@id/gl_v_db_list_last"/>
<TextView
android:id="@+id/tvDecklistNumber"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="x3"
android:autoSizeMaxTextSize="16sp"
android:autoSizeMinTextSize="8sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:textColor="#ffff"
cardview:layout_constraintEnd_toEndOf="parent"
cardview:layout_constraintStart_toStartOf="@id/gl_v_db_list_last" />
<android.support.constraint.Guideline
android:id="@+id/gl_v_db_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
cardview:layout_constraintGuide_begin="20dp" />
<android.support.constraint.Guideline
android:id="@+id/gl_v_db_list_secound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
cardview:layout_constraintGuide_begin="40dp" />
<android.support.constraint.Guideline
android:id="@+id/gl_v_db_list_third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
cardview:layout_constraintGuide_begin="55dp" />
<android.support.constraint.Guideline
android:id="@+id/gl_v_db_list_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
cardview:layout_constraintGuide_end="15dp" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
我认为你只是从错误的地方得到了
rvList
,它不应该在你的视图持有者中,它应该在你的片段中,这就是为什么当你点击它时它会抛出NullPointerException
首先,从视图保持器中删除rvList=introListView.findViewById(R.id.rv_deckbuilder_list);
,因为它只返回null
接下来,更新您的deckbuilder\u RViewAdapter\u列表
适配器,使其能够支持数据更改:
public deckbuilder_RViewAdapter_List (Context mContext, List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
this.mContext = mContext;
}
public void setCards(List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
notifyDataSetChanged();
}
最后,在片段中找到RecyclerView
s,然后用适当的适配器连接它们:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// ... after inflate view
rvCards = view.findViewById(R.id.rv_deckbuilder_cards)
rvList = view.findViewById(R.id.rv_deckbuilder_list)
deckbuilder_RViewAdapter_List listAdapter = new deckbuilder_RViewAdapter_List(..)
deckbuilder_RViewAdapter_Card cardAdapter = new deckbuilder_RViewAdapter_Card(.., listAdapter)
rvCards.setAdapter(cardAdapter);
rvList.setAdapter(listAdapter);
// ...
}
问题似乎出在“rvList”上。它是什么?在哪里给它赋值?@theblitz在我的公共类Viewholder中它正在初始化。@theblitz它正在初始化为RecyclerView rvList;以及。嘿@Aaron我实现了你的建议,但我仍然有相同的错误。我想将LinearLayoutManager设置为一个新的RecyclerView。我想使用find的地方ViewById位于第一个Recyclerview的Viewholder中,因为我正在使用第一个Recyclerview中的一个按钮调用新的Recyclerview…但我仍然有相同的错误..还有其他建议吗?我能帮你做点什么吗?总之,我对上下文的概念还不熟悉。因为我认为我的视图在片段中,所以我需要获取上下文我错了吗?谢谢你到目前为止的帮助。好的,你能和我分享一下布局图,以及包括rv的布局图吗?我做到了。希望这对@Aaron有帮助。:)我想我知道你在做什么。首先,你肯定不需要这些行
rvList=introListView、 findViewById(R.id.rv_deckbuilder_list);
因为我认为它不在项目视图中。接下来,您可能想将rvList
传递给适配器。@Christian.gruener我已经更新了我的答案,希望它对您有用。
public deckbuilder_RViewAdapter_List (Context mContext, List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
this.mContext = mContext;
}
public void setCards(List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
notifyDataSetChanged();
}
private deckbuilder_RViewAdapter_List mCardListAdapter;
public deckbuilder_RViewAdapter_Card(FragmentActivity mContext, List<Cards> mData, deckbuilder_RViewAdapter_List mCardListAdapter) {
this.mData = mData;
this.mContext = mContext;
this.mCardListAdapter = mCardListAdapter;
}
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
fab_deckbuilder_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Decklist> listCards = new ArrayList<>();
listCards.add(new Decklist( ... );
mCardListAdapter.setCards(listCards);
}
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// ... after inflate view
rvCards = view.findViewById(R.id.rv_deckbuilder_cards)
rvList = view.findViewById(R.id.rv_deckbuilder_list)
deckbuilder_RViewAdapter_List listAdapter = new deckbuilder_RViewAdapter_List(..)
deckbuilder_RViewAdapter_Card cardAdapter = new deckbuilder_RViewAdapter_Card(.., listAdapter)
rvCards.setAdapter(cardAdapter);
rvList.setAdapter(listAdapter);
// ...
}