【问题标题】:Android gallery view keeps crashing and scrolls really slowly. What's wrong with my codeAndroid 图库视图不断崩溃并且滚动非常缓慢。我的代码有什么问题
【发布时间】:2014-07-17 10:29:21
【问题描述】:

基本上,我有一个简单的画廊视图,可以滚动浏览图像列表,但是当我在三星 Galaxy s3 mini(不是模拟器)上运行该程序时,当我滑动时它滚动浏览图像的速度非常慢并且有很多滞后.我滚动几秒钟后它也会崩溃。我究竟做错了什么???我最终想将它实现到另一个具有更多图像和更多滚动的应用程序中。这应该可以工作,因为我使用的是我的手机而不是模拟器,所以它不应该是性能问题。它在代码中。在我的 logcat 中,它说“致命信号 11”有人可以帮忙吗?这是我的代码。

列表适配器:

package com.example.nowlpractice;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryImageAdapter extends BaseAdapter 
{
private Context mContext;

private Integer[] mImageIds = {
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
        R.drawable.koala,
};

public GalleryImageAdapter(Context context) 
{
    mContext = context;
}

public int getCount() {
    return mImageIds.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}


// Override this method according to your need
public View getView(int index, View view, ViewGroup viewGroup) 
{
    // TODO Auto-generated method stub
    ImageView i = new ImageView(mContext);

    i.setImageResource(mImageIds[index]);
    i.setLayoutParams(new Gallery.LayoutParams(200, 200));

    i.setScaleType(ImageView.ScaleType.FIT_XY);

    return i;
}
}

活动:

package com.example.nowlpractice;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class NwActivity extends Activity 
{

 ImageView selectedImage;  
 private Integer[] mImageIds = {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.koala,
            R.drawable.pic5,
            R.drawable.pic4,
    };
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nw);

         Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    selectedImage=(ImageView)findViewById(R.id.imageView1);
    gallery.setSpacing(1);
    gallery.setAdapter(new GalleryImageAdapter(this));

     // clicklistener for Gallery
    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(NwActivity.this, "Your selected position = " + position,           
Toast.LENGTH_SHORT).show();
            // show the selected Image
            selectedImage.setImageResource(mImageIds[position]);
        }
    });
}
}

和 XML:

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

 <ImageView
    android:id="@+id/imageView1"
    android:layout_marginTop="100dp"
    android:layout_width="250dp"
    android:layout_gravity="center_horizontal"
    android:layout_height="250dp"
    android:src="@drawable/pic1" />

 </LinearLayout>

这是我的 LOGCAT

07-17 12:24:11.360: I/dalvikvm(13610):   at   
android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-17 12:24:11.360: I/dalvikvm(13610):   at   
android.view.Choreographer.doFrame(Choreographer.java:523)
07-17 12:24:11.360: I/dalvikvm(13610):   at 
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-17 12:24:11.360: I/dalvikvm(13610):   at 
android.os.Handler.handleCallback(Handler.java:615)
07-17 12:24:11.360: I/dalvikvm(13610):   at a
android.os.Handler.dispatchMessage(Handler.java:92)
07-17 12:24:11.360: I/dalvikvm(13610):   at android.os.Looper.loop(Looper.java:137)
07-17 12:24:11.360: I/dalvikvm(13610):   at   
android.app.ActivityThread.main(ActivityThread.java:4867)
07-17 12:24:11.360: I/dalvikvm(13610):   at   
java.lang.reflect.Method.invokeNative(Native Method)
07-17 12:24:11.360: I/dalvikvm(13610):   at j   
java.lang.reflect.Method.invoke(Method.java:511)
07-17 12:24:11.360: I/dalvikvm(13610):   at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-17 12:24:11.360: I/dalvikvm(13610):   at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-17 12:24:11.360: I/dalvikvm(13610):   at dalvik.system.NativeStart.main(Native    
Method)
07-17 12:24:11.360: A/libc(13610): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1),    
thread     
13610 (le.nowlpractice)

【问题讨论】:

  • 你能发布异常的堆栈跟踪吗?顺便说一下,Gallery 已被弃用,最好使用 ViewPager 代替它
  • @Chaosit ViewPager 会和画廊一样工作吗???我发布了我的logcat。谢谢

标签: android android-imageview baseadapter android-gallery listadapter


【解决方案1】:

你的 logcat 看起来像 OutOfMemory

只是一些问题: 你的图片尺寸是多少?

您是否尝试过不使用 imageView 而是使用 TextView?..(仅用于测试)

你在 Galaxy s3 mini 上的版本是什么?

当然,即使功能相似,也请尝试使用 viewpager...

【讨论】:

    【解决方案2】:

    替换这个构造函数

    mContext = context;
       TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
                itemBackground = a.getResourceId(
                        R.styleable.Gallery1_android_galleryItemBackground, 0);
                a.recycle();public GalleryImageAdapter(Context context) 
    
    create like this resource 
    
     <resources>
         <declare-styleable name="Gallery1">
          <attr name="android:galleryItemBackground"></attr>
        </declare-styleable>
      </resources>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多