总是报空指针,已经闭眼摸索好多天了,求大牛们帮忙指教
MainActivity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private List<String> mDatas; private SimpleAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initDatas(); initView(); mAdapter = new SimpleAdapter(this,mDatas); mRecyclerView.setAdapter(mAdapter); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecyclerView.setLayoutManager(linearLayoutManager); } private void initView() { mRecyclerView = (RecyclerView)findViewById(R.id.id_recyclerview); } private void initDatas() { mDatas = new ArrayList<String>(); for (int i = 'A';i < 'z';i++){ mDatas.add(""+(char)i); } } }
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; /** * Created by Administrator on 2016/4/17. */ public class SimpleAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<String> mDatas; private LayoutInflater mInflater; private Context mContext; @Override public int getItemCount() { return mDatas.size(); } @Override public void onBindViewHolder(MyViewHolder holder, int pos) { holder.tv.setText(mDatas.get(pos)); } @Override public MyViewHolder onCreateViewHolder(ViewGroup arg0, int viewType) { View view = mInflater.inflate(R.layout.item_single_textview,arg0,false); MyViewHolder hodler = new MyViewHolder(arg0); return hodler; } @Override public long getItemId(int position) { return super.getItemId(position); } public SimpleAdapter(Context context,List<String> datas) { this.mContext = context; this.mDatas = datas; mInflater = LayoutInflater.from(context); } } class MyViewHolder extends RecyclerView.ViewHolder{ TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView)itemView.findViewById(R.id.tv); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.qdbl.recyclerviewdemo2.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="72dp" android:background="#44ff0000"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" /> </FrameLayout>