2013-02-18 36 views
0

我正在嘗試使用下面的代碼在彈出窗口中顯示圖像。基本上有一個音頻文件列表,當我點擊它時,出現一個彈出窗口,顯示其專輯封面(這是我期望的)。但是,該應用程序崩潰。 LogCat給出了NullPointerException--我確信這是因爲ImageView而已。這裏是我的代碼:因ImageView導致應用程序崩潰NullPointerException

public class MainActivity extends SherlockListActivity { 
    Uri sourceUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    @SuppressWarnings("deprecation") 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);   
     @SuppressWarnings("unused") 
     ActionBar ab = getSupportActionBar(); 
     String[] from = {MediaStore.MediaColumns.TITLE,MediaStore.MediaColumns.DATA}; 
     int[] to = {android.R.id.text1}; 

     CursorLoader cursorLoader = new CursorLoader(this,sourceUri,null,null,null,MediaStore.Audio.Media.TITLE); 
     Cursor cursor = cursorLoader.loadInBackground(); 
     startManagingCursor(cursor); 
     ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,from,to,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
     setListAdapter(adapter); 

    } 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     Cursor cl = ((SimpleCursorAdapter)l.getAdapter()).getCursor(); 
     cl.moveToPosition(position); 
     String loc = cl.getString(cl.getColumnIndex(MediaStore.MediaColumns.DATA)); 
     File f = new File(loc); 
     MusicMetadataSet mmds; 
     try { 
      mmds = new MyID3().read(f); 
      if(mmds!=null){ 
      MusicMetadata mmd = (MusicMetadata)mmds.getSimplified(); 
      String s = mmd.getArtist() +"-"+mmd.getSongTitle(); 
      Toast.makeText(this,s,Toast.LENGTH_SHORT).show(); 
      @SuppressWarnings("unchecked") 
      Vector<ImageData> img = mmd.getPictureList(); 
       if(!img.isEmpty()){ 
        byte[] imgData = img.get(0).imageData; 
        Bitmap bmp = BitmapFactory.decodeByteArray(imgData, 0, imgData.length); 
        LayoutInflater layoutInflater=(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
        View popupView = layoutInflater.inflate(R.layout.popup, null); 
        final PopupWindow popupWindow = new PopupWindow(popupView,100,100); 

           /**** PROBLEM BECAUSE OF THIS ************/ 
        ImageView iv = (ImageView)findViewById(R.id.imageView); 
        iv.setImageBitmap(bmp); 



        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 
        Button btnOk = (Button)popupView.findViewById(R.id.btnOk); 
        btnOk.setOnClickListener(new OnClickListener() 
        {      
         @Override 
         public void onClick(View v) 
         { 
          popupWindow.dismiss(); //dismissing the popup 
         } 
        }); 


       } 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

我該如何解決它?這裏是我的popup.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:background="#90FFFFFF" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:text="@string/art" 
     android:textColor="#000000" 
     android:gravity="center" 
     android:textStyle="bold" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:id="@+id/imageView" 
     android:contentDescription="@string/art" 
     android:layout_height="wrap_content" 
     /> 
    <Button 
     android:layout_width="fill_parent" 
     android:id="@+id/btnOk" 
     android:text="@string/ok" 
     android:layout_height="wrap_content"> 
    </Button> 
</LinearLayout> 

的事情是,在TextViewButton出現就好了。這個問題奇怪的發生在ImageView!請幫忙,這讓我瘋狂。

回答

3

代碼

ImageView iv = (ImageView)findViewById(R.id.imageView); 

更改爲

ImageView iv = (ImageView)popupView.findViewById(R.id.imageView); 
+0

是的,這是做得最常見的錯誤之一。 – Raanan 2013-02-18 09:19:23

+0

當!它的工作:D:D感謝一堆。唷。我可以在10分鐘內接受。 – h4ck3d 2013-02-18 09:19:26