我正在为我的应用创建一个简单的ViewPager图库。这是我到目前为止实施的代码。
这是我的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.newgallery.MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="240dp" />
</LinearLayout>
这是我的java文件
public class MainActivity extends Activity {
private Bitmap[] IMAGES;
Bitmap bit1,bit2,bit3,bit4;
String getid, url1,url2,url3,url4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getid = "14";
url1 = "http://example.com/dom/uploads/"+getid+"/img1.jpg";
url2 = "http://example.com/dom/uploads/"+getid+"/img2.jpg";
url3 = "http://example.com/dom/uploads/"+getid+"/img3.jpg";
url4 = "http://example.com/dom/uploads/"+getid+"/img4.jpg";
bit1 = LoadImages(url1);
bit2 = LoadImages(url2);
bit3 = LoadImages(url3);
bit4 = LoadImages(url4);
IMAGES = new Bitmap[]{bit1,bit2,bit3,bit4};
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter();
viewPager.setAdapter(adapter);
}
class ImageAdapter extends PagerAdapter {
@Override
public int getCount() {
return IMAGES.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageBitmap(IMAGES[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Bitmap result;
public Bitmap LoadImages(String url){
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
imageLoader.get(url, new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
Bitmap drawimg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
result = drawimg;
}
@Override
public void onResponse(ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
result = response.getBitmap();
}
}
});
return result;
}
}
这是我的Logcat。 所有链接都正常运行。在这里,我将原始域名更改为example.com
09-02 19:36:35.568: D/Volley(630): [76] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://example.com/dom/uploads/14/img2.jpg 0x90e75755 LOW 2> [lifetime=3027], [size=107746], [rc=200], [retryCount=0]
09-02 19:36:35.568: D/Volley(630): [74] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://example.com/dom/uploads/14/img1.jpg 0x90e75755 LOW 1> [lifetime=3075], [size=89388], [rc=200], [retryCount=0]
09-02 19:36:35.798: D/dalvikvm(630): GC_FOR_ALLOC freed 281K, 4% free 19781K/20487K, paused 84ms, total 85ms
09-02 19:36:35.808: I/dalvikvm-heap(630): Grow heap (frag case) to 20.002MB for 544016-byte allocation
09-02 19:36:35.918: D/dalvikvm(630): GC_FOR_ALLOC freed <1K, 4% free 20312K/21063K, paused 101ms, total 101ms
09-02 19:36:36.028: D/dalvikvm(630): GC_CONCURRENT freed <1K, 4% free 20312K/21063K, paused 30ms+22ms, total 100ms
09-02 19:36:36.108: D/Volley(630): [1] Request.finish: 4019 ms: [ ] http://example.com/dom/uploads/14/img3.jpg 0x90e75755 LOW 3
09-02 19:36:36.218: D/dalvikvm(630): GC_FOR_ALLOC freed 18K, 4% free 20313K/21063K, paused 69ms, total 69ms
09-02 19:36:36.228: I/dalvikvm-heap(630): Grow heap (frag case) to 20.522MB for 544016-byte allocation
09-02 19:36:36.318: D/dalvikvm(630): GC_FOR_ALLOC freed 0K, 4% free 20845K/21639K, paused 84ms, total 84ms
09-02 19:36:36.428: D/dalvikvm(630): GC_CONCURRENT freed 1K, 4% free 20845K/21639K, paused 30ms+9ms, total 98ms
09-02 19:36:36.568: D/Volley(630): [1] Request.finish: 4472 ms: [ ] http://example.com/dom/uploads/14/img4.jpg 0x90e75755 LOW 4
09-02 19:36:36.638: D/dalvikvm(630): GC_FOR_ALLOC freed 20K, 4% free 20845K/21639K, paused 63ms, total 64ms
09-02 19:36:36.648: I/dalvikvm-heap(630): Grow heap (frag case) to 21.041MB for 544016-byte allocation
09-02 19:36:36.728: D/dalvikvm(630): GC_FOR_ALLOC freed 0K, 4% free 21376K/22215K, paused 77ms, total 77ms
09-02 19:36:36.908: D/Volley(630): [1] Request.finish: 4813 ms: [ ] http://example.com/dom/uploads/14/img2.jpg 0x90e75755 LOW 2
09-02 19:36:36.978: D/Volley(630): [1] Request.finish: 4897 ms: [ ] http://example.com/dom/uploads/14/img1.jpg 0x90e75755 LOW 1
所以我的问题是当我第一次尝试运行我的应用程序时它没有加载图像。我得到4个白页。但如果我退出程序并再次运行它,我可以看到所有图像都加载得很好。
之后,我只需按下后退按钮,再次点击启动器图标启动应用程序。
我在这里想念什么?有人可以解释一下这个原因吗?
答案 0 :(得分:0)
这不是上述问题的确切答案。但我找到了另一种选择。它被称为Universal Image Loader for Android。使用这个库我设法在这种情况下幸存下来。