我創建了一個ViewPager,我在其中使用bitmapArray在viewpager中顯示圖像。每件事情都可以正常工作,但是當我添加新數據並調用notifydatasetchange時,應用程序崩潰。當向viewpager添加新數據時,應用程序崩潰
在這個應用程序中,我正在下載歌曲,並從該文件中獲取下載的歌曲的位圖並添加到位圖數組中。
它提供了以下Execption:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我無法找到是什麼原因導致這種execption
適配器類ViewPager是:
public class CoverFlowPagerAdapter extends PagerAdapter {
ArrayList<Bitmap> mBitmapArray;
Context c;
interface CoverFlowClick {
public void coverClick(int coverFlowPosition);
}
CoverFlowClick mCoverFlowClick;
public CoverFlowPagerAdapter(Context c, ArrayList<Bitmap> mBitmapArray) {
this.c = c;
this.mBitmapArray = mBitmapArray;
mCoverFlowClick = (CoverFlowClick) c;
}
@Override
public int getCount() {
return mBitmapArray.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (object == view);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView imageView;
View v = null;
LayoutInflater inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.coverflow_item,null);
imageView= (ImageView) v.findViewById(R.id.coverFlowImage);
imageView.setImageBitmap(mBitmapArray.get(position));
container.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c, "Clicked::--" + position, Toast.LENGTH_SHORT).show();
mCoverFlowClick.coverClick(position);
}
});
return imageView;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
}
我加入了位圖陣列並調用notifydataset像這樣更改:
@Override
public void updateSongList(String songName) {
getBitmapArray(songName);
mCoverFlowAdapter.notifyDataSetChanged();
}
編輯:我的自定義佈局文件是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/coverFlowImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
和我getBitmapArray功能
public void getBitmapArray(String songName) {
Bitmap bitmap = null;
path = Environment.getExternalStorageDirectory() + "/AppDownloads/" + songName;
File file = new File(path);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
metaRetriver.setDataSource(inputStream.getFD());
} catch (IOException e) {
e.printStackTrace();
}
art = metaRetriver.getEmbeddedPicture();
if (art != null) {
bitmap = BitmapFactory
.decodeByteArray(art, 0, art.length);
}
if (bitmap != null) {
mBitmapArray.add(bitmap);
} else {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.unknown_album);
mBitmapArray.add(icon);
}
}
V = inflater.inflate(R.layout.coverflow_item,容器,FALSE);嘗試這個。 –
plz add ** getBitmapArray ** code –
檢查我的編輯@RahulSharma – AbhayBohra