上下文变为null对于嵌入在对话框中的gridview

时间:2013-06-21 02:29:41

标签: android gridview imageview gallery

我正在尝试让Dialog显示已捕获照片的图库但在运行时会出现gv.setAdapter(new EOrderGalleryAdapter(getApplicationContext(), data));处的Null Exception。

是否有关于使用默认设置打开外部SD卡中文件照片的提示或说明 - 图库或其他?

以下是我的代码

        final Dialog dialog = new Dialog(getBaseContext());

        //remove the default title if you want to
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //here is where we inflate our custom xml containing a gridview
        dialog.setContentView(R.layout.gallery);                    
        String sdCardRootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()+"/TVB_PV_photo";
        File rootFolder = new File(sdCardRootPath);
        File[] picFiles = rootFolder.listFiles();
        ArrayList<Bitmap> data = new ArrayList<Bitmap>();
        for (File pic:picFiles) {

            //Bitmap b= BitmapFactory.decodeFile(pic.getAbsolutePath());
            Bitmap b =  decodeFile(pic,4);
            data.add(b);
        }
        GridView gv = (GridView) findViewById(R.id.grid_view);




        gv.setAdapter(new EOrderGalleryAdapter(getApplicationContext(), data));
        gv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // TODO Auto-generated method stub
                   Toast.makeText(getBaseContext(), 
                            "photo" + (position + 1) + " selected", 
                            Toast.LENGTH_SHORT).show();
            }});                    

Logcat

    06-21 10:19:50.946: E/AndroidRuntime(3925):     at android.view.View$1.onClick(View.java:3125)
06-21 10:19:50.946: E/AndroidRuntime(3925):     ... 11 more
06-21 10:19:50.946: E/AndroidRuntime(3925): Caused by: java.lang.NullPointerException
06-21 10:19:50.946: E/AndroidRuntime(3925):     at atvpv.test.OrderSystem1.EOSCustPayment.onCreateDialog(EOSCustPayment.java:352)
06-21 10:19:50.946: E/AndroidRuntime(3925):     at android.app.Activity.onCreateDialog(Activity.java:2828)
06-21 10:19:50.946: E/AndroidRuntime(3925):     at android.app.Activity.createDialog(Activity.java:944)
06-21 10:19:50.946: E/AndroidRuntime(3925):     at android.app.Activity.showDialog(Activity.java:2921)
06-21 10:19:50.946: E/AndroidRuntime(3925):     at android.app.Activity.showDialog(Activity.java:2880)
06-21 10:19:50.946: E/AndroidRuntime(3925):     at atvpv.test.OrderSystem1.EOSCustPayment.buttonGallery(EOSCustPayment.java:421)

1 个答案:

答案 0 :(得分:1)

我认为问题不在于getApplicationContext()

合理地,当您调用gv时,空指针应为gv.setAdapter,因此它检测到gv为null并引发异常。