2013-02-18 83 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

相关问题