我正在尝试使用下面的代码在弹出窗口中显示图像。基本上有一个音频文件列表,当我点击它时,会出现一个弹出窗口,显示它的专辑封面(这是我期望它做的)。但是,应用程序崩溃了。LogCat 给出了一个NullPointerException
——我相信这是ImageView
唯一的。这是我的代码:
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>
问题是,TextView
和Button
看起来就好了。奇怪的是,ImageView
只有!请帮忙,这让我发疯了。