Created my recyclerView and it works fine. Also created a onClick listener for the Cardview. All working fine but when i intent and set some resource for the next activity from recyclerView. I am getting an error of Null object resourse. That too i found that's because of i used wrong layout view which i have inflated in onCreateViewHolder.
- Now i need to inflate a view in ChestAdapter but i cant do that if so tell some suggestions
- Or i need to getAdapterposition in my onCreate java file of second activity called Chestinfo if i got that my setResourse method is working fine
- Here are my code
my Chest activty code
public class Chest_activity extends AppCompatActivity { public RecyclerView mRecyclerView; public RecyclerView.Adapter mAdapter; public RecyclerView.LayoutManager mLayoutManager; public String[] myDataset = {"Barbell Bench press","Dumbell Flat bench press","Cross cable","incline Flyes"}; public int[] logos = {R.mipmap.barbell_icon, R.mipmap.cross_icon, R.mipmap.dips_icon, R.mipmap.dumbbell}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chest_activity); // initializing Recycler view mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example) mAdapter = new ChestAdapter(myDataset,logos); mRecyclerView.setAdapter(mAdapter); } } here is the Adapter code
public class ChestAdapter extends RecyclerView.Adapter<ChestAdapter.ViewHolder> { public String[] mDataset; public int[] mlogo; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { // each data item is just a string in this case public TextView mTextView; public CardView cardView; public ImageView logo; public Context context; public ViewHolder(View v) { super(v); cardView = (CardView) v.findViewById(R.id.card_view); mTextView = (TextView) cardView.findViewById(R.id.workout_name); logo = (ImageView) cardView.findViewById(R.id.logo); context = cardView.getContext(); cardView.setOnClickListener(this); } @Override public void onClick(View v) { ImageView imageView; TextView textView; imageView = (ImageView)v.findViewById(R.id.workout_image); textView = (TextView)v.findViewById(R.id.workout_details); Intent intent; switch (getAdapterPosition()){ case 0: intent = new Intent(context, Chest_info.class); imageView.setImageResource(R.drawable.barbell_bench); textView.setText(R.string.Barbel_info); context.startActivity(intent); break; case 1: intent = new Intent(context, Chest_info.class); imageView.setImageResource(R.drawable.dumbell); textView.setText(R.string.dumbel); context.startActivity(intent); break; case 2: intent = new Intent(context, Chest_info.class); imageView.setImageResource(R.drawable.cross); textView.setText(R.string.flies); context.startActivity(intent); break; default: intent = new Intent(context, Chest_info.class); imageView.setImageResource(R.drawable.barbell_bench); textView.setText(R.string.Barbel_info); context.startActivity(intent); break; } } } // Provide a suitable constructor (depends on the kind of dataset) public ChestAdapter(String[] myDataset, int[] logos) { mDataset = myDataset; mlogo = logos; } // Create new views (invoked by the layout manager) @Override public ChestAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType ) { // create a new view View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_frame, parent, false); // set the view's size, margins, paddings and layout parameters return new ViewHolder(v); } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { // - get element from your dataset at this position // - replace the contents of the view with that element holder.mTextView.setText(mDataset[position]); holder.logo.setImageResource(mlogo[position]); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.length; } } here is my second activity code (Chest_info )
public class Chest_info extends AppCompatActivity { public ImageView imageView; public TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chest_info); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); imageView = (ImageView) findViewById(R.id.workout_image); textView = (TextView) findViewById(R.id.workout_details); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; getWindow().setLayout((int) (width * .8), (int) (height * .8)); } } literally i cant use getAdapterPosition any where else except ViewHolder class
even i tried changing class into interface to implement that shows another error
here is my chest info XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/chest_info" android:background="@android:color/transparent"> <android.support.v7.widget.CardView app:cardElevation="0dp" app:cardCornerRadius="6dp" android:layout_height="match_parent" android:layout_width="match_parent" android:backgroundTintMode="screen" android:id="@+id/info_card" tools:context=".Chest_info"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/workout_image" android:layout_width="match_parent" android:layout_height="200dp"> </ImageView> <TextView android:id="@+id/workout_details" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </android.support.v7.widget.CardView> Also here is my error log
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.studentzone.fitaholic_, PID: 31203 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at com.example.studentzone.fitaholic_.ChestAdapter$ViewHolder.onClick(ChestAdapter.java:62) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19761) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5264) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
No comments:
Post a Comment