我有麻煩使用通用的圖像加載程序庫從自定義網址獲取圖像中的片段,這裏是我使用的代碼: 我不知道爲什麼它不工作,因爲它應該工作。任何建議的幫助將不勝感激!加載圖像
最好的問候,
public class Fragment2 extends Fragment {
public Fragment2(){}
String[] imageUrls;
AbsListViewBaseActivity aa = new AbsListViewBaseActivity();
DisplayImageOptions options;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container, false);
Bundle bundle = getActivity().getIntent().getExtras();
imageUrls = bundle.getStringArray(Extra.IMAGES);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
aa.listView = (GridView) rootView.findViewById(R.id.gridview);
((GridView) aa.listView).setAdapter(new ImageAdapter());
aa.listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
private void startImagePagerActivity(int position) {
Intent intent = new Intent(getActivity(), ImagePagerActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
intent.putExtra(Extra.IMAGE_POSITION, position);
startActivity(intent);
}
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
aa.imageLoader.displayImage(imageUrls[position], holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current,
int total) {
holder.progressBar.setProgress(Math.round(100.0f * current/total));
}
}
);
return view;
}
class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
}
代碼佈局:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip"
android:padding="4dip" />
和清單文件:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.loadimage.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="zeal.kingdom.koreamania.Fragment1"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.loadimage.Fragment2"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.loadimage.Fragment3"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.loadimage.Fragment4"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.loadimage.Fragment5"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.loadimage.Fragment6"
android:label="@string/app_name"
android:configChanges="locale|orientation|keyboardHidden"
android:noHistory="true" >
</activity>
<activity
android:name="com.example.loadimage.Fragment7"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.loadimage.MainActivity"
android:label="@string/app_name" >
</activity>
</application>
日誌貓:
Thread [<1> main] (Suspended (exception NullPointerException))
<VM does not provide monitor information>
Fragment2.onCreateView(LayoutInflater, ViewGroup, Bundle) line: 42
Fragment2(Fragment).performCreateView(LayoutInflater, ViewGroup, Bundle) line: 1700
FragmentManagerImpl.moveToState(Fragment, int, int, int, boolean) line: 890
FragmentManagerImpl.moveToState(int, int, int, boolean) line: 1062
BackStackRecord.run() line: 684
FragmentManagerImpl.execPendingActions() line: 1447
FragmentManagerImpl$1.run() line: 443
Handler.handleCallback(Message) line: 733
Handler.dispatchMessage(Message) line: 95
Looper.loop() line: 136
ActivityThread.main(String[]) line: 5017
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 515
ZygoteInit$MethodAndArgsCaller.run() line: 779
ZygoteInit.main(String[]) line: 595
NativeStart.main(String[]) line: not available [native method]
42: imageUrls = bundle.getStringArray(Extra.IMAGES);
1700: return onCreateView(inflater, container, savedInstanceState);
890: f.mView = f.performCreateView(f.getLayoutInflater(
moveToState(f, newState, transit, transitStyle, false);
任何答案裏面片段歡迎從互聯網加載的圖片! –
你如何設置你的'ImageLoader'實例和'RequestQueue'實例?有沒有任何理由你不使用'NetworkImageView'? –
嗨,亞當,我在此期間切換到通用圖像加載器,希望修復,但仍然出現錯誤。我更新了我使用的代碼:)非常感謝您的幫助! 儘管如此,任何在片段中使用gridview的代碼都可以。 –