0

I'm using Picasso Library to load images from several Urls in a GridView. Now I want to be able to see the images in fullscreen, for so I added a clickListener. The problem is in the FullScreen Activity in this line: fi.setImageResource(gv.getItemId(position)) -> setImageResource (int) in TouchImageView cannot be applied to (long)

public class FullScreen extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        Intent i = getIntent();

        int position = i.getExtras().getInt("id");
        SampleGridViewAdapter gv = new SampleGridViewAdapter(this);

        final TouchImageView fi = (TouchImageView) findViewById(R.id.full_img);
        fi.setImageResource(gv.getItemId(position));

        Picasso.with(getApplicationContext())
                .load(gv.getItem(position))
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .fit().centerInside()
                .into(fi);
    }
}

Also check this link, that guy had the same issue but I can't find a way to fix it: Android Picasso GridView - Store URL data in Integer array?

EDIT: - this is what I get now when I run the app.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zenyt/com.zenyt.FullScreen}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.zenyt.FullScreen.onCreate(FullScreen.java:16) // line: int position = i.getExtras().getInt("id");
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)

This is the fragment from where I call the FullScreen activity:

public class Audi2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(com.zenyt.R.layout.audi2, container, false);

        GridView gv = (GridView) rootView.findViewById(R.id.grid_view);
        gv.setAdapter(new SampleGridViewAdapter(getActivity()));
        gv.setOnScrollListener(new SampleScrollListener(getActivity()));

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent mainIntent = new Intent(getActivity(), FullScreen.class);
                startActivity(mainIntent);
            }
        });

       return rootView;
    }

Sorry for my english.

Community
  • 1
  • 1
Alec
  • 347
  • 2
  • 9
  • 29

2 Answers2

1

gv.getItemId() will return the id of the grid view, which is a long. Not the resource id. Consider getting the resource another way. Maybe you meant to do this instead?

fi.setImageResource(gv.getItem(position));  //not getItemId

Unless you have set your gv.getItemId() to return the resource id. If that is the case, then just cast to int.

 fi.setImageResource((int) gv.getItemId(position));
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • The second line ( casting the int ) doesn't give me any error but when I launch the app and press on an image, it crashes. I think it's because of this line `int position = i.getExtras().getInt("id");` . I'm doing something wrong. Do you have any idea? – Alec Jan 31 '15 at 19:26
  • I see. How are you starting this activity? – Chad Bingham Jan 31 '15 at 19:28
  • I added the fragment from which I call the FullScreen activity. – Alec Jan 31 '15 at 19:47
  • When you call the activity are you adding the bundle to the activity? post that code – Chad Bingham Jan 31 '15 at 19:48
  • Should I do something like this http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity ? – Alec Jan 31 '15 at 20:02
  • Yes, exactly. I figured you were. – Chad Bingham Jan 31 '15 at 20:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69967/discussion-between-binghammer-and-masterz-yuri). – Chad Bingham Jan 31 '15 at 20:07
0

It looks like you are not passing in the bundle to your activity. So when you try to get "id" it is not there. Create and add the bundle in your onClick like so:

@Override
public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
      //create bundle
      Bundle bundle = new Bundle();
      //add data to your bundle
      bundle.putInt("id", position);
      //create intent
      Intent mainIntent = new Intent(getActivity(), FullScreen.class);
      //add bundle to intent
      mainIntent.putExtras(bundle);
      //start activity
      startActivity(mainIntent);
}
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115