我试图将AlbumArt放在ListView中的ImageView中。 我没有意识到为什么第一个例子正在工作,我可以在其他片段中将位图放在ImageView中,但我不能将位图放在listview中。
BookFragment.mAlbumArt.setImageBitmap(bitmap);'
正在运作,而BookFragment'是其他片段
ivListAlbumArt.setImageBitmap(bitmap);
不起作用
代码
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.library, parent, false);
ListView lvLibList = (ListView)view.findViewById(R.id.lvLibList);
ImageButton btAddItem = (ImageButton)view.findViewById(R.id.btAddItem);
lvLibList.setAdapter(adapter);
final ImageView ivListAlbumArt = (ImageView)view.findViewById(R.id.ivListAlbumArt);
mBooks = BookLib.get(getActivity()).getBooks();
lvLibList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Book b = (Book)parent.getAdapter().getItem(position);
String fn = b.getBookFileName();
Log.d(LOG_TAG, fn);
musicSrv.releaseMedia();
musicSrv.initMediaPlayer();
musicSrv.setMedia(fn);
musicSrv.createMedia();
musicSrv.playNewMedia();
BookFragment.mFileName.setText(musicSrv.getMediaTitle());
byte[] art = musicSrv.getMediaAlbumArt();
if(art != null)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
BookFragment.mAlbumArt.setImageBitmap(bitmap);
BookFragment.mAlbumArt.setAdjustViewBounds(true);
BookFragment.mAlbumArt.setLayoutParams(new LinearLayout.LayoutParams(800, 800));
ivListAlbumArt.setImageBitmap(bitmap);
ivListAlbumArt.setAdjustViewBounds(true);
ivListAlbumArt.setLayoutParams(new LinearLayout.LayoutParams(80, 80));
}
else
{
BookFragment.mAlbumArt.setImageResource(R.drawable.thevoice);
BookFragment.mAlbumArt.setAdjustViewBounds(true);
BookFragment.mAlbumArt.setLayoutParams(new LinearLayout.LayoutParams(800, 800));
ivListAlbumArt.setImageResource(R.drawable.thevoice);
ivListAlbumArt.setAdjustViewBounds(true);
ivListAlbumArt.setLayoutParams(new LinearLayout.LayoutParams(80, 80));
}
}
});
BookAdapter bookAdapter = new BookAdapter(mBooks);
lvLibList.setAdapter(bookAdapter);
btAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenFileDialog fileDialog = new OpenFileDialog(getActivity());
fileDialog.show();
}
});
return view;
}
UPD:适配器代码。 我可以在适配器中添加更新ImageView的代码吗?
private class BookAdapter extends ArrayAdapter<Book> {
public BookAdapter(ArrayList<Book> books) {
super(getActivity(),0,books);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item, null);
}
Book b = getItem(position);
TextView tvTitle = (TextView)convertView.findViewById(R.id.tvFileName);
TextView tvPath = (TextView)convertView.findViewById(R.id.tvFilePath);
TextView tvListDuration = (TextView)convertView.findViewById(R.id.tvListDuration);
ImageView ivListAlbumArt = (ImageView)convertView.findViewById(R.id.ivListAlbumArt);
tvTitle.setText(b.getTitle());
tvPath.setText(b.getBookAutor());
return convertView;
}
}
适配器XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_weight="0"
android:id="@+id/ivListAlbumArt"
android:padding="3dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/tvFileName"
android:text="FileName will be here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="5dp"
android:textStyle="bold"
android:layout_toRightOf="@id/ivListAlbumArt"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/tvListDuration"
android:text="0:00"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/tvFilePath"
android:text="FilePath will be here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="@id/tvFileName"
android:layout_toRightOf="@id/ivListAlbumArt"
android:padding="5dp"/>
</RelativeLayout>
UPD:LogCat输出
11-14 07:08:31.742 5943-5943/ru.app.a_book W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4159aba8)
11-14 07:08:31.742 5943-5943/ru.app.a_book E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ru.app.a_book, PID: 5943
java.lang.NullPointerException
at ru.app.a_book.TabLibrary$2.onItemClick(TabLibrary.java:182) // code: ivListAlbumArt.setImageBitmap(bitmap);
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
at android.widget.AbsListView$3.run(AbsListView.java:3645)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您尚未在任何地方初始化ivListAlbumArt
,这就是您获得空指针异常的原因
在ivListAlbumArt
中初始化setOnItemClickListener
:
lvLibList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ivListAlbumArt = (ImageView) view.findViewById(R.id.ivListAlbumArt);
}
});