android 组件RecyclerView的使用指教?

图片描述

想在首页使用RecyclerView来显示数据,因为使用了fragment所以我想在返回首页这一部分是填充数据,但是一直报错,这个控件不是太熟悉可以看看代码哪里有问题


MainActivaty

package com.example.fiz.myapplication;

import android.graphics.Color;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v4.app.Fragment;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.RecyclerView.ViewHolder;

import android.support.v7.widget.Toolbar;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.LinearLayout;

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.LinearLayout;

import android.support.v7.widget.Toolbar;

import android.widget.TextView;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private ViewPager mPager;

private ArrayList<Fragment> fragmentsList;

private LinearLayout ll_top;

//首页组件定义

private RecyclerView mRecyclerView;

private List<String> mDatas;

private HomeAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitle("demo");

toolbar.setSubtitle("1234");

toolbar.setLogo(R.drawable.ic_launcher);

setSupportActionBar(toolbar);

initView();

}

private void initView() {

ll_top = (LinearLayout) findViewById(R.id.ll_top);

mPager = (ViewPager) findViewById(R.id.vPager);

fragmentsList = new ArrayList<Fragment>();

//首页个recelerView添加数据

// initData();

// mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

// mRecyclerView.setAdapter(mAdapter = new HomeAdapter());

//

Fragment activityfragment = TestFragment.newInstance("界面一");

Fragment groupFragment = TestFragment.newInstance("界面2");

Fragment friendsFragment = TestFragment.newInstance("界面3");

Fragment chatFragment = TestFragment.newInstance("界面4");

Fragment about = About.about("1234");

Fragment homepage = HomePage.homePage("首页");

fragmentsList.add(homepage);/**首页**/

fragmentsList.add(groupFragment);

fragmentsList.add(friendsFragment);

fragmentsList.add(chatFragment);

mPager.setAdapter(new MyFragmentPagerAdapter(

getSupportFragmentManager(), fragmentsList));

mPager.setCurrentItem(0);

BottomTabBar navigationBar = new BottomTabBar(this);

ll_top.setBackgroundColor(Color.parseColor("#F8F8FF"));

navigationBar.attachToParent(ll_top, new String[]{"首页", "消息", "发现",

"我"}, mPager);

}

protected void initData() {

mDatas = new ArrayList<String>();

for (int i = 'A'; i < 'z'; i++) {

mDatas.add("this is a demo---" + (char) i);

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

private ArrayList<Fragment> fragmentsList;

public MyFragmentPagerAdapter(FragmentManager fm) {

super(fm);

}

public MyFragmentPagerAdapter(FragmentManager fm,

ArrayList<Fragment> fragments) {

super(fm);

this.fragmentsList = fragments;

}

@Override

public int getCount() {

return fragmentsList.size();

}

@Override

public Fragment getItem(int arg0) {

return fragmentsList.get(arg0);

}

@Override

public int getItemPosition(Object object) {

return super.getItemPosition(object);

}

}

class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>

{

@Override

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

{

MyViewHolder holder = new MyViewHolder(LayoutInflater.from(

MainActivity.this).inflate(R.layout.item_home, parent,

false));

return holder;

}

@Override

public void onBindViewHolder(MyViewHolder holder, int position)

{

holder.tv.setText(mDatas.get(position));

}

@Override

public int getItemCount()

{

return mDatas.size();

}

class MyViewHolder extends ViewHolder

{

TextView tv;

public MyViewHolder(View view)

{

super(view);

tv = (TextView) view.findViewById(R.id.id_num);

}

}

}

}

布局文件

home_page

<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"

tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView

android:id="@+id/my_recycler_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="vertical"/>

</RelativeLayout>

item_home

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:background="#44ff0000"

android:layout_height="wrap_content" >

<TextView

android:id="@+id/id_num"

android:layout_width="match_parent"

android:layout_height="50dp"

android:gravity="center"

android:text="1" />

</FrameLayout>

HomePage

@SuppressLint("ValidFragment")

public class HomePage extends Fragment {

private String textString;

//首页组件

public HomePage(String textString) {

this.textString = textString;

}

public static HomePage homePage(String textString) {

HomePage myabout = new HomePage(textString);

return myabout;

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.home_page, container, false);

return view;

}

}

异常报错信息

03-19 23:41:20.809 3818-3818/? E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.fiz.myapplication, PID: 3818

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fiz.myapplication/com.example.fiz.myapplication.MainActivity}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)

at android.app.ActivityThread.-wrap11(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5417)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: 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.fiz.myapplication.MainActivity.initView(MainActivity.java:77)

at com.example.fiz.myapplication.MainActivity.onCreate(MainActivity.java:64)

at android.app.Activity.performCreate(Activity.java:6237)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 

at android.app.ActivityThread.-wrap11(ActivityThread.java) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 

at android.os.Handler.dispatchMessage(Handler.java:102) 

at android.os.Looper.loop(Looper.java:148) 

at android.app.ActivityThread.main(ActivityThread.java:5417) 

at java.lang.reflect.Method.invoke(Native Method) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

回答:

这句不能注释啊

// mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

我有个RecyclerView使用的Demo,可以看看

回答:

ava.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.fiz.myapplication.MainActivity.initView(MainActivity.java:77)

我觉得你需要看懂是哪里错误分析log。这里的错误信息写的很清楚MainActivity.java的77行有一个空指针错误

以上是 android 组件RecyclerView的使用指教? 的全部内容, 来源链接: utcz.com/p/175870.html

回到顶部