首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RecyclerView上的空例外

RecyclerView上的空例外
EN

Stack Overflow用户
提问于 2016-06-06 18:36:13
回答 1查看 1.5K关注 0票数 1

我所要做的就是在一个表格布局中创建一个回收视图,我已经尝试在主活动中设置适配器,以及在表格布局中使用的片段im,但是,我仍然得到了这个错误。

无法启动活动ComponentInfo{knightsrealms.managment_app/knightsrealms.managment_app.Dashboard}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法的空android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)‘

代码语言:javascript
运行
复制
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);
}
}

这是我的适配器

代码语言:javascript
运行
复制
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();
}
}

此代码用于表布局中的片段。

代码语言:javascript
运行
复制
<android.support.v7.widget.RecyclerView
android:id="@+id/rvcal"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

这是主要的活动xml。

代码语言:javascript
运行
复制
<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>

任何对此的见解/帮助都是非常感谢的,谢谢!!

EN

回答 1

Stack Overflow用户

发布于 2016-06-07 03:50:49

你是否已经尝试过:

代码语言:javascript
运行
复制
tabLayout.setupWithViewPager(viewPager);

因为recyclerView在片段xml中,而不是在活动xml中,所以recyclerView是返回null。好的,您的代码几乎完成了,但是您还需要2件事情:

  • 首先,是一个为您的FragmentPagerAdapter扩展ViewPager的类,我不知道您的ViewPageAdapter类是否已经这样做了,但是让我们假设您还没有这样做。
  • 第二,是FragmentPagerAdapter的片段。

Oke让我们开始:

  1. 为FragmentContacts创建FragmentPagerAdapter,在这个片段中,您应该管理RecyclerView和它的适配器,而不是在活动中。 公共类ArrayList扩展片段{ ArrayList(5);联系人=新的ArrayList(5);@Nullable @重写公共视图onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState) { View rootView = ( View ) inflater.inflate(R.layout.your_fragment_xml,容器,false);contacts.add(0,新knightsrealms.managment_app.Calendar(“new 1",true));contacts.add(1,新knightsrealms.managment_app.Calendar(“数字2",真));CalendarAdapter适配器=新CalendarAdapter(联系人);RecyclerView rvContacts = (RecyclerView) rootView.findViewById(R.id.rvcal);rvContacts.setLayoutManager(新LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));rvContacts.setAdapter(适配器);返回rootView;}}
  2. 最后,回到您的仪表板活动中,在onCreate中,setUp您的查看器: @重载受保护的onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_dashboard);toolbar工具栏= ( Toolbar ) findViewById(R.id.tool_bar);viewPager = (ViewPager) findViewById(R.id.viewpager);viewPagerAdapter =新ViewPageAdapter(getSupportFragmentManager();viewPager.setAdapter(viewPagerAdapter);viewPager.setAdapter=(ViewPagerAdapter)();();(0);} 公共类ViewPagerAdapter扩展了FragmentPagerAdapter {公共ViewPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager);} @重写公共片段getItem(int位置){if( == 0)返回新的FragmentContacts();if(位置== 1)返回新的FragmentContacts();抛出新的IllegalStateException(“意外位置”+位置);}@覆盖公共int getCount() {返回2;}@覆盖公共CharSequence getPageTitle(int位置){if(位置== 0)返回“消息”;如果(位置== 1)返回“仪表板”;抛出新的IllegalStateException(“意外位置”+位置);} }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37664565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档