2015-08-28 54 views
7

我試圖從SD卡中獲取圖像到位圖中顯示Image-view.After應用程序的前兩到三個圖像顯示,但當我滾動列表應用程序崩潰,並獲得NullPointerException異常:java.lang.NullPointerException:嘗試調用虛擬方法'int android.graphics.Bitmap.getWidth()'null對象引用 在com.example.tazeen.classnkk.Activity1 $ MyListAdapter.getView(Activity1.java:357)Android:NullPointerException:嘗試調用空虛對象的虛擬方法'int android.graphics.Bitmap.getWidth()'參考

   if(objElement.endsWith(mp3_Pattern)) 
       { 
        Log.e("Mp3 ", " ends with "); 

        { 
         int scalFactor = fixHeight/h ; 
         newWidth = (int)(w * scalFactor); 
         Log.e("scalFactor "," = " + scalFactor); 
         Log.e("newWidth "," = " + newWidth); 
         Log.e("resizing... ","in if condition"); 
         new_Height = fixHeight ; 
        } 
        else 
        { 
         newWidth = w; 
         new_Height = h; 
        } 


        Uri uri = Uri.fromFile(new File(objElement)); 
        Picasso.with(getContext()).load(uri).resize(newWidth, new_Height).placeholder(R.drawable.img_placeholder).into(imageView , new com.squareup.picasso.Callback() { 
         @Override 
         public void onSuccess() { 
          if (pBar != null) { 
           pBar.setVisibility(View.GONE); 
          } 
         } 
         @Override 
         public void onError() { 

         } 
        }); 
       } 

       holder.linearLayout.addView(imageView); 
       holder.linearLayout.addView(pBar); 

這裏是記錄了信息離子

08-28 14:22:14.077 2947-2947/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.tazeen.classnkk, PID: 2947 
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 
      at com.example.tazeen.classnkk.AllPosts_Page$MyListAdapter.getView(AllPosts_Page.java:357) 
      at android.widget.AbsListView.obtainView(AbsListView.java:2347) 
      at android.widget.ListView.makeAndAddView(ListView.java:1864) 
      at android.widget.ListView.fillDown(ListView.java:698) 
      at android.widget.ListView.fillGap(ListView.java:662) 
      at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4991) 
      at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3418) 
      at android.widget.AbsListView.onTouchMove(AbsListView.java:3801) 
      at android.widget.AbsListView.onTouchEvent(AbsListView.java:3632) 
      at android.view.View.dispatchTouchEvent(View.java:8471) 
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399) 
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092) 
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405) 
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106) 

回答

2

嘗試。您 內,如果(objElement.endsWith(png_Pattern))

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap bitmap = BitmapFactory.decodeFile(objElement, options); 
+0

但在我的字符串objElement中:已經獲取sd卡路徑,如objElement:= /mnt/sdcard/SDImages/95_20150824112324025.png。 – androidTag

+0

我試過上面的代碼,但bitmapSize爲空爲什麼 – androidTag

+0

我已更新的答案也參考http://stackoverflow.com/a/8710690/1552136。如果沒有解決這個讓我知道我會試試這個。 – harshitpthk

1

嘗試用

File file = new File(objElement); 
    if (file.exists()) { 
    Bitmap bitmapSize = BitmapFactory.decodeFile(file.getAbsolutePath()); 
    } 
+0

你們爲什麼用'新文件(objElement).getAbsolutePath()'?爲什麼不''file.getAbsolutePath()'? – CoolMind

0

更換Bitmap bitmapSize = BitmapFactory.decodeFile(objElement);看來,你有無法解碼圖像。

從技術文檔:

/** 
* Decode a file path into a bitmap. If the specified file name is null, 
* or cannot be decoded into a bitmap, the function returns null. 
* 
* @param pathName complete path name for the file to be decoded. 
* @return the resulting decoded bitmap, or null if it could not be decoded. 
*/ 
public static Bitmap decodeFile(String pathName) { 
    return decodeFile(pathName, null); 
} 
0

我知道我有點晚了這個答案。不過今天我面臨着同樣的問題。使用位圖在畢加索設置圖像,並沒有解決我的問題。我嘗試了下面的一個。

Picasso.with(holder.mRootView.getContext()).load(new File(trackingNumbers.get(position).getImageUrl())).error(R.drawable.no_image_place_holder).noFade().placeholder(R.drawable.no_image_place_holder).into(circularImageView); 

謝謝

相關問題