在选中的水平RecyclerView项目上更新ListView项目数据

时间:2017-01-25 11:06:45

标签: java android listview nullpointerexception android-recyclerview

我想更新水平RecyclerView textview位置选择的列表项数据。

当我设置适配器时,我得到一个Null Pointer Exception,ListView为null。

我的代码如下:

activity_main.xml中:

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

   <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".ChapterList"
android:id="@+id/relativeroot"
android:background="@drawable/bgimage">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/toolbarbg"
    android:minHeight="?attr/actionBarSize">

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back_white" />

    <Textview
        android:id="@+id/toolbartitle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="@color/white" />

    <ImageView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginRight="10dp"
        android:src="@drawable/play" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginLeft="80dp"
    android:layout_below="@+id/toolbar"
    android:id="@+id/viewpager">
    </android.support.v7.widget.RecyclerView>

<ListView
    android:id="@+id/list_chapter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/viewpager"
    android:layout_marginTop="10dp" />

MainActivity.java

         mListView.setAdapter(chapterListItem);
         RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);

PagerAdaptre.java

      public class PagerAdapter extends 
       RecyclerView.Adapter<PagerAdapter.MyViewHolder> {
        Context context;
        ArrayList<Chapter> arrayList;
         Mainactivity mainactivity;

        public PagerAdapter(Context context, ArrayList<Chapter> arrayList) {
           this.context = context;
           this.arrayList = arrayList;
        }


public class MyViewHolder extends RecyclerView.ViewHolder{
         public TextView textView;
          public ImageView imageView;

          public MyViewHolder(View view){
             super(view);
             textView=(TextView)view.findViewById(R.id.tv_chapter_no);
             imageView=(ImageView)view.findViewById(R.id.img_selected);
               }
           }
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Viewitemview=LayoutInflater.from(parent.getContext())
        .inflate(R.layout.viewpager_item,parent,false);
    return new MyViewHolder(itemview);
    }

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {

    final Chapter chapter = arrayList.get(position);
    holder.textView.setText("" + chapter.getChapternumber());
    if (position == 0) {
        holder.imageView.setImageResource(R.drawable.pagination);
    }
    holder.textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            holder.imageView.setImageResource(R.drawable.pagination);
            mainactivity=new MainActivity();
              mainactivity.setpos
             (Integer.parseInt(""+chapter.getChapternumber()));
        }

    });

}


   @Override
  public int getItemCount() {
      return arrayList.size();
 }
 }

MainActivity.java

void setpos(int position) {

    chapterpos=position;//chappos is int variable
    setlistdata(chapterpos);
}
private void setlistdata(int position){
    namelist.clear();
    chapterListItem=null;
    chapterListItem = new ChapterListItem(ChapterList.this, namelist);
    try {


        chapterListItem.notifyDataSetChanged();
        mListView.setAdapter(chapterListItem);// i am getting 
        listview null here

     } catch (Exception e) {
        e.printStackTrace();
    }
}

我的Logcat如下:

01-25 17:11:39.904 26929-26929/com.ex.Appname

W/System.err: java.lang.NullPointerException: 
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
01-25 17:11:39.904 26929-26929/
 W/System.err:     at MainActivity.setlistdata(MainActivity.java:625)
01-25 17:11:39.904 26929-26929/

W/System.err:     at MainActivity.setpos(ChapterList.java:581)
01-25 17:11:39.904 26929-26929/

W/System.err:     at PagerAdapter$1.onClick(PagerAdapter.java:61)
01-25 17:11:39.904 26929-26929/

W/System.err:     at android.view.View.performClick(View.java:4783)
01-25 17:11:39.904 26929-26929/

W/System.err:     at android.view.View$PerformClick.run(View.java:19887)
01-25 17:11:39.904 26929-26929/

W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
01-25 17:11:39.904 26929-26929/

W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-25 17:11:39.904 26929-26929/

W/System.err:     at android.os.Looper.loop(Looper.java:135)
01-25 17:11:39.904 26929-26929/

 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5290)
01-25 17:11:39.904 26929-26929/

W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-25 17:11:39.904 26929-26929/

W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
01-25 17:11:39.904 26929-26929/

W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
01-25 17:11:39.904 26929-26929/

W/System.err:     atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

3 个答案:

答案 0 :(得分:0)

您是否在MainActivity中找到了listview(以xml声明)? 包括以下行 -

mListView = (ListView)findViewById(R.id.your_listview_id)

答案 1 :(得分:0)

private void setlistdata(int position){
    namelist.clear(); // **You are clearing the nameList here, so It might possible you should add new data before use it again.**
    chapterListItem=null;
    chapterListItem = new ChapterListItem(ChapterList.this, namelist);
try {

    chapterListItem.notifyDataSetChanged();
    mListView.setAdapter(chapterListItem);// i am getting 
    listview null here

 } catch (Exception e) {
    e.printStackTrace();
}

}

答案 2 :(得分:0)

mainactivity=new MainActivity();

您刚刚创建了一个新的Activity,并且丢失了对您使用findViewById的所有引用。

经验法则:永远不要进行new活动。您需要以不同方式处理所选项目。理想情况下,它是从Activity而不是Adapter中设置它,以便您可以调用必要的方法

要记住的一件事:你怎么会“回去”一章?点击后,你重新加载整个适配器,丢失旧数据,所以我猜你还没想过呢