Al hacer clic en una vista de imagen de la actividad principal para cambiar a la segunda actividad, se bloquea. Podría cambiar a la segunda actividad hasta que implemente la vista de reciclaje. Supongo que hay algún tipo de problema con el administrador de diseño.

activity_second.xml

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<android.support.v7.widget.RecyclerView

    android:id="@+id/recyclerView"

    android:layout_width="match_parent"

    android:layout_height="match_parent" />

HomeItemAdapter.java

public class HomeItemAdapter extends RecyclerView.Adapter<HomeItemAdapter.ViewHolder> {
private static final String TAG = "NTR-HomeItemAdapter";

//    private String[] mDataSet;
public ArrayList<HashMap<String, String>> mDataSource;
private Context mContext;

public HomeItemAdapter(Context context, ArrayList<HashMap<String, String>> mDataSource) {
    this.mDataSource = mDataSource;
    this.mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_home_item, viewGroup, false);

    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
    Target target;
    Log.d(TAG, "Element " + position + " set.");

    viewHolder.getName().setText(mDataSource.get(position).get("name"));
    viewHolder.getCity().setText(mDataSource.get(position).get("city"));

    //Get Marquee
    viewHolder.getName().setSingleLine();
    viewHolder.getName().setEllipsize(TextUtils.TruncateAt.MARQUEE);
    viewHolder.getName().setHorizontallyScrolling(true);
    viewHolder.getName().setFocusableInTouchMode(true);
    viewHolder.getName().setSelected(true);
    viewHolder.getName().setMarqueeRepeatLimit(-1);

    viewHolder.getCity().setSingleLine();
    viewHolder.getCity().setEllipsize(TextUtils.TruncateAt.MARQUEE);
    viewHolder.getCity().setHorizontallyScrolling(true);
    viewHolder.getCity().setFocusableInTouchMode(true);
    viewHolder.getCity().setSelected(true);
    viewHolder.getCity().setMarqueeRepeatLimit(-1);

    // Get Dynamic Color
    target = new com.squareup.picasso.Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
                public void onGenerated(Palette palette) {
                    if (palette !=null) {
                        viewHolder.getImageCover().setImageBitmap(bitmap);
                        Palette.Swatch vibrant = palette.getVibrantSwatch();
                        if(vibrant!=null) {
                            viewHolder.getCardItemHome().setBackgroundColor(vibrant.getRgb());
                            viewHolder.getName().setTextColor(vibrant.getTitleTextColor());
                            viewHolder.getCity().setTextColor(vibrant.getTitleTextColor());
                        }
                    }
                }
            });
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            viewHolder.getImageCover().setImageResource(R.drawable.cover);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };
    Log.d(TAG,mDataSource.get(position).get("image_url"));

    Picasso.with(mContext)
            .load(mDataSource.get(position).get("image_url"))
            .into(target);

}

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

public static class ViewHolder extends RecyclerView.ViewHolder {
    private final CardView cardItemHome;
    private final ImageView imgCover;
    private final TextView txtName;
    private final TextView txtCity;

    public ViewHolder(View v) {
        super(v);
        // Define click listener for the ViewHolder's View.
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Element " + getPosition() + " clicked.");
            }
        });
        cardItemHome = (CardView) v.findViewById(R.id.card_item_home);
        txtName = (TextView) v.findViewById(R.id.txt_name);
        txtCity = (TextView) v.findViewById(R.id.txt_city);
        imgCover = (ImageView) v.findViewById(R.id.img_cover);

    }

    public CardView getCardItemHome() {
        return cardItemHome;
    }

    public TextView getName() {
        return txtName;
    }

    public TextView getCity() {
        return txtCity;
    }

    public ImageView getImageCover() {
        return imgCover;
    }

  }

}

SecondActivity.java

public class SecondActivity extends ActionBarActivity {

public ArrayList<HashMap<String, String>> mDataSource;

protected RecyclerView mRecyclerView;
protected RecyclerView.LayoutManager mLayoutManager;
protected HomeItemAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initialize Data Source
    initDataset();

    // BEGIN_INCLUDE(initializeRecyclerView)
    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    //The LayoutManager defines how elements are laid out.
    mLayoutManager = new GridLayoutManager(this, 3);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new HomeItemAdapter(this, mDataSource);
    // Set CustomAdapter as the adapter for RecyclerView.
    mRecyclerView.setAdapter(mAdapter);
    // END_INCLUDE(initializeRecyclerView)
}


private void initDataset() {
    mDataSource = new ArrayList();
    HashMap<String, String> h1 = new HashMap();
    h1.put("name", "FIFTH HARMONY");
    h1.put("city", "Reflection");
    h1.put("image_url","http://www.azlyrics.com/hot/fifthharmony_B00MS4BUUU.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "PAPA ROACH");
    h1.put("city", "F.E.A.R.");
    h1.put("image_url","http://www.azlyrics.com/hot/919y0earTjL.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "Ellie Goulding");
    h1.put("city", "ドラえもん");
    h1.put("image_url","http://cdn1.umg3.net/12/files/2013/07/EG_HD_stan.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "SAYWECANFLY");
    h1.put("city", "Between The Roses");
    h1.put("image_url","http://www.azlyrics.com/hot/61-TWobzipL.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "NE-YO");
    h1.put("city", "Non-Fiction");
    h1.put("image_url","http://www.azlyrics.com/hot/81kBXMLAHkL.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "Frozen");
    h1.put("city", "Frozen Heart Lyrics");
    h1.put("image_url","http://www.stlyrics.com/images/ama/frozen_6430.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "2NE1");
    h1.put("city", "K-Pop Superstar");
    h1.put("image_url","http://www.billboard.com/files/styles/promo_310/public/media/cl_2ne1_instagram_kpop2014_650-430a.png");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "PAPA ROACH");
    h1.put("city", "F.E.A.R.");
    h1.put("image_url","http://www.azlyrics.com/hot/919y0earTjL.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "SAYWECANFLY");
    h1.put("city", "Between The Roses");
    h1.put("image_url","http://www.azlyrics.com/hot/61-TWobzipL.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "NE-YO");
    h1.put("city", "Non-Fiction");
    h1.put("image_url","http://www.azlyrics.com/hot/81kBXMLAHkL.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "Frozen");
    h1.put("city", "Frozen Heart Lyrics");
    h1.put("image_url","http://www.stlyrics.com/images/ama/frozen_6430.jpg");
    mDataSource.add(h1);

    h1 = new HashMap();
    h1.put("name", "2NE1");
    h1.put("city", "K-Pop Superstar");
    h1.put("image_url","http://www.billboard.com/files/styles/promo_310/public/media/cl_2ne1_instagram_kpop2014_650-430a.png");
    mDataSource.add(h1);
  }

}

Logcat

Process: com.example.sensei.mist, PID: 2193
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sensei.mist/com.example.sensei.mist.SecondActivity}: 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:2325)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                       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:903)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                    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.sensei.mist.SecondActivity.onCreate(SecondActivity.java:33)
                                                                       at android.app.Activity.performCreate(Activity.java:5990)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:135) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                       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:903) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
1
Vortex 24 jul. 2016 a las 21:04

2 respuestas

La mejor respuesta

En SecondActivity, cambie

setContentView(R.layout.activity_main);

Para

setContentView(R.layout.activity_second);
3
Karakuri 24 jul. 2016 a las 18:15

En su segunda clase de actividad de Java, el método oncreate está inflando el diseño principal de la actividad y su RecyclerView está en la actividad second.xml

Así que cambia setcontentview

0
Ankesh kumar Jaisansaria 24 jul. 2016 a las 18:21