2013-06-21 41 views
0

我試圖讓對話框顯示捕獲的照片的畫廊,但在此運行時,發生空的異常,在gv.setAdapter(new EOrderGalleryAdapter(getApplicationContext(), data));發生。上下文變爲空對於嵌入在對話框中的griview

是否有任何關於使用默認設置打開位於外部SD卡中的文件的照片或圖片 - Gallery或其他?下面

是我的代碼

 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) 
+0

用'gv.getContext()替換'getApplicationContext()'' – ozbek

回答

1

我不認爲這個問題是getApplicationContext()

合理的空指針應該是gv,因爲你在調用gv.setAdapter所以它檢測到gv是空的並拋出異常。

相關問題