我有使用tablayout的Viewpager所以我希望第二个TAB2是带有图像和文本的回收者视图我正在尝试这样做但是总是在tab2中获得nullexception
String[] names={"Icardi","Candreva","Milito","Perisic","Zanetti"};
String[] positions={"Striker","Winger", "Striker","Winger","Defender"};
int[] images={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,};
TextView textView,textView2;
ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2, container, false);
RecyclerView rv= (RecyclerView)v.findViewById(R.id.myRecycler);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
MyAdapter adapter=new MyAdapter(getActivity(),names,positions,images);
rv.setAdapter(adapter);
return v;
}
myAdapter
public class MyAdapter extends RecyclerView.Adapter<MyHolder>{
Context c;
String[] players;
String[] positions;
int[] images;
public MyAdapter(Context ctx,String[] players,String[] positions,int[] images)
{
this.c=ctx;
this.players=players;
this.positions=positions;
this.images=images;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Intent i=new Intent(c,MiniDetails.class);
i.putExtra("Name",players[position]);
i.putExtra("Position",positions[position]);
i.putExtra("Image",images[position]);
c.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return players.length;
}
myholder
public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img;
TextView nameTxt,posTxt;
ItemClickListener itemClickListener;
public MyHolder(View itemView) {
super(itemView);
img = (ImageView)itemView.findViewById(R.id.playerImage);
nameTxt = (TextView) itemView.findViewById(R.id.nameTxt);
posTxt = (TextView)itemView.findViewById(R.id.posTxt);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v,getLayoutPosition());
}
public void setItemClickListener(ItemClickListener ic)
{
this.itemClickListener=ic;
}
listFragment.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="android.support.v7.widget.RecyclerView "/>
model.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playerImage"
android:padding="10dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="@+id/nameTxt"
android:padding="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/playerImage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="position"
android:id="@+id/posTxt"
android:padding="10dp"
android:layout_alignBottom="@+id/playerImage"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
tab2.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
我很绝望请有人帮帮我,,,!或指导我。!
logcat的
FATAL EXCEPTION: main
Process: com.example.mike.projectx, PID: 14918
java.lang.NullPointerException
at com.example.mike.projectx.FirstTabs.Tab2.onCreateView(Tab2.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
at android.view.View.measure(View.java:16518)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:16518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:16518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2410)
at android.view.View.measure(View.java:16518)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1921)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1114)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1296)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1001)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5680)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
at dalvik.system.NativeStart.main(Native Method)