getExtras NullPointerException

时间:2017-02-08 07:48:40

标签: android

应用程序崩溃NullPointerException行 int position = i.getExtras()。getInt(" id"); 可能是什么问题?请告诉我!

我以教程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;
    }
}

错误

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) 

4 个答案:

答案 0 :(得分:1)

尝试以下代码:

替换:

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

有了这个:

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

然后:

Intent i = getIntent();
Bundle extras = i.getExtras();
int position = extras.getInt("id");

答案 1 :(得分:0)

使用它:

Bundle bundle = getIntent().getExtras();
int position = bundle.getInt("id");

答案 2 :(得分:0)

方法getIntent()返回null,或者i.getExtras()返回null。尝试调试它,看看它是哪一个。然后尝试弄清楚为什么它返回null并修复它或在适当时检查null。

答案 3 :(得分:0)

您应该将它用作getIntExtra()

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

不要混淆“1”,它的默认值为