-1

app crash NullPointerException in line int position = i.getExtras().getInt("id"); what could be the problem ? Tell me please !

I build on the tutorial http://seegatesite.com/android-tutorial-display-image-to-android-gridview-from-url-using-picasso

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                i.putExtra("id", position);
                startActivity(i);

FullImageActivity

public class FullImageActivity extends Activity {

ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullimageview);



        Intent i = getIntent();

    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    img = (ImageView) findViewById(R.id.image);
    String url = imageAdapter.getItem(position);

    new DownloadImage().execute(url);
}
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... URL) {
        String imageURL = URL[0];
        Bitmap bitmap = null;
        try {
            InputStream input = new java.net.URL(imageURL).openStream();
            bitmap = BitmapFactory.decodeStream(input);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        img.setImageBitmap(result);
    }
}

ImageAdapter

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    int imageTotal = 7;
    public static String[] mThumbIds = {
            "http://i.imgur.com/KzQO.jpg",
            "http://i.imgur.com/87KE.jpg",
            "http://i.imgur.com/HeJ.jpg",
            "http://i.imgur.com/3d3.jpg",
            "http://i.imgur.com/WLi.jpg",
            "http://i.imgur.com/Pp.jpg",
            "http://i.imgur.com/Ldt.jpg",
    };

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return imageTotal;
    }

    @Override
    public String getItem(int position) {
        return mThumbIds[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        String url = getItem(position);
        Picasso.with(mContext)
                .load(url)
                .placeholder(R.drawable.loader)
                .fit()
                .centerCrop().into(imageView);
        return imageView;
    }
}

Errors

java.lang.RuntimeException: Unable to start activity ComponentInfo{123.myapplication/123.myapplication.FullImageActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at 123.myapplication.FullImageActivity.onCreate(FullImageActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
at android.app.ActivityThread.access$700(ActivityThread.java:159) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:176) 
at android.app.ActivityThread.main(ActivityThread.java:5419) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
at dalvik.system.NativeStart.main(Native Method) 
V01ume
  • 3
  • 4

4 Answers4

1

Try out the following code:

Replace :

Intent intent = new Intent(getApplicationContext(), FullImageActivity.class);  

With this:

Intent intent = new Intent(MainActivity.this, FullImageActivity.class);   

And then:

Intent i = getIntent();
Bundle extras = i.getExtras();
int position = extras.getInt("id");
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
0

use this :

Bundle bundle = getIntent().getExtras();
int position = bundle.getInt("id");
Deepak Kumar
  • 1,035
  • 10
  • 18
0

Either the method getIntent() returns null, or i.getExtras() returns null. Try to debug it and see which one it is. Then try to figure out why it returns null and fix it or check for null if appropriate.

vstrom coder
  • 297
  • 1
  • 8
0

You should use it as getIntExtra()

int position = getIntent().getIntExtra("id",1);

don't get confuse about that "1", its in default value

Sasi
  • 445
  • 4
  • 19