对不起,如果我的英语不是很完美。
我正在学习Java和Android Studio两周,我试图添加滑动标签,并在标签内,一个带有CardViews的RecyclerView作为一个有名称和图片的行项目。 我的滑动标签工作,我可以将卡片视图放在标签中,带有图片和名称。但是,当我使用RecyclerView时,它不起作用,我有一个错误。我从昨天起就无法解决这个问题。
这是我的节目。
我的班级人员
public class Person {
String name;
int photoId;
public Person(String name, int photoId) {
this.name = name;
this.photoId = photoId;
}
private List<Person> persons;
private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("Benjamin", R.drawable.team));
persons.add(new Person("Thomas", R.drawable.haltere));
}
//GETTER
public String getName() {
return this.name;
}
public int getPhoto() {
return this.photoId;
}
我的类适配器
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardView);
personName = (TextView)itemView.findViewById(R.id.cardTitle);
personPhoto = (ImageView)itemView.findViewById(R.id.cardPhoto);
}
}
List<Person> persons;
public RVAdapter(List<Person> persons){
this.persons = persons;
}
@Override
public int getItemCount() {
return persons.size();
}
@Override
public PersonViewHolder onCreateViewHolder (ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
return new PersonViewHolder(v);
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
我的活动
public class ActivityHome extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"1","2","3","4"};
int Numboftabs =4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView rv = (RecyclerView)findViewById(R.id.my_recycler_view);
ArrayList<Person> persons = new ArrayList<>();
RVAdapter rAdapter = new RVAdapter(persons);
rv.setAdapter(rAdapter);
rv.setLayoutManager(layoutManager);
}
(之后有几个代码,但它仅用于Sliding Tabs,并且有效。)
我的recycler_view.xml
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
我的activity_home.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
我的cardview.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#ffffff"
app:cardCornerRadius="5dp"
app:cardElevation="7dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityHome"
android:background="#ffffff">
<TextView
android:id="@+id/cardTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Card Title"
android:textColor="#000000"
android:textSize="17sp" />
<ImageView
android:id="@+id/cardPhoto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="50dp"
android:src="@drawable/haltere"
android:scaleType="center"
/>
</RelativeLayout>
我的错误:
Process: com.android.frutii.fitness5, PID:640 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.frutii.fitness5/com.android.frutii.fitness5.ActivityHome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at com.android.frutii.fitness5.ActivityHome.onCreate(ActivityHome.java:45)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
我在互联网上阅读了很多答案,但无法摆脱这个错误。
答案 0 :(得分:1)
setContentView();
方法正在使用Activity.findViewById()
设置的布局中搜索my_recycler_view
。因此,您应该将setContentView()
移至RecyclerView
布局。
答案 1 :(得分:0)
activity_home布局中不存在ID为RecyclerView
的{{1}}。使用R.id.my_recycler_view
作为R.layout.recycler
答案 2 :(得分:0)
我移动我的RecyclerView.xml,我将代码放在activity_home.xml中,如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
</android.support.v4.view.ViewPager>
现在我部分工作,我有4个标签,我也有一张卡片,但只有一个:(为什么我的第二个不在这里?也许这不是好事而且我不会&#39不得不朝着这个方向前进..?