【发布时间】:2021-01-22 04:06:23
【问题描述】:
我想要做的就是在 tablayout 中创建一个 recyclerview 我已经尝试在主要活动中设置适配器以及我在 tablayout 中使用的片段但是无论哪种方式我仍然得到这个错误
java.lang.RuntimeException:无法启动活动 ComponentInfo{knightsrealms.managment_app/knightsrealms.managment_app.Dashboard}:java.lang.NullPointerException:尝试调用虚拟方法 'void android.support.v7.widget.RecyclerView.setAdapter (android.support.v7.widget.RecyclerView$Adapter)' 在空对象引用上
package knightsrealms.managment_app;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import java.util.ArrayList;
import java.util.Calendar;
public class Dashboard extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPageAdapter viewPagerAdapter;
ArrayList<knightsrealms.managment_app.Calendar> contacts = new ArrayList<knightsrealms.managment_app.Calendar>(5);
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
viewPagerAdapter = new ViewPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
setSupportActionBar(toolbar);
final TabLayout.Tab messages = tabLayout.newTab();
final TabLayout.Tab dashboard = tabLayout.newTab();
messages.setText("Messages");
dashboard.setText("Dashboard");
tabLayout.addTab(dashboard, 0);
tabLayout.addTab(messages, 1);
tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
RecyclerView rvContacts = (RecyclerView)findViewById(R.id.rvcal);
contacts.add(0, new knightsrealms.managment_app.Calendar("Number 1",true));
contacts.add(1, new knightsrealms.managment_app.Calendar("Number 2",true));
CalendarAdapter adapter = new CalendarAdapter(contacts);
rvContacts.setAdapter(adapter);
rvContacts.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dashboard_menu, menu);
return true;
}
void selectPage(int pageIndex){
tabLayout.setScrollPosition(pageIndex,0f,true);
viewPager.setCurrentItem(pageIndex);
}
}
这是我的适配器
package knightsrealms.managment_app;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.ViewHolder> {
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView nameTextView;
public Button messageButton;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
messageButton = (Button) itemView.findViewById(R.id.message_button);
}
}
private List<Calendar> mContacts;
// Pass in the contact array into the constructor
public CalendarAdapter(List<Calendar> contacts) {
mContacts = contacts;
}
// Usually involves inflating a layout from XML and returning the holder
@Override
public CalendarAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.recyclercell, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(CalendarAdapter.ViewHolder viewHolder, int position) {
// Get the data model based on position
Calendar contact = mContacts.get(position);
// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(contact.getName());
Button button = viewHolder.messageButton;
if (contact.isOnline()) {
button.setText("Message");
button.setEnabled(true);
}
else {
button.setText("Offline");
button.setEnabled(false);
}
}
// Return the total count of items
@Override
public int getItemCount() {
return mContacts.size();
}
}
此代码适用于 tablayout 内的片段
<android.support.v7.widget.RecyclerView
android:id="@+id/rvcal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是主要的活动 xml
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
></include>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:layout_below="@+id/tool_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:tabIndicatorHeight="3dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
/>
</RelativeLayout>
非常感谢您对此的任何见解/帮助!
【问题讨论】:
-
这一行
viewPager.setAdapter(viewPagerAdapter);产生错误,导致viewPager 为空。请分享您的 xml,以便我们查看您在那里做了什么以及错误在哪里。 -
@Vucko 我刚刚用 xml 更新了问题
-
你说这个xml是
main_activity.xml,但是在你的Dashboard活动中你是setContentView(R.layout.activity_dashboard);。我想也许你正试图在错误的活动中访问 viewpager。你能检查一下吗?什么是 MainActivity,什么是 Dasboard?
标签: nullpointerexception android-recyclerview