1

好的,我正在尝试设置一个图像幻灯片,它会在某个暂停后自动更新。我尝试通过使用 Handler 和 timer 类来实现它。但它不起作用。请帮帮我。

代码:

    import java.util.Timer;
    import java.util.TimerTask;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.animation.AnimationUtils;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.Gallery.LayoutParams;
    import android.widget.ImageSwitcher;
    import android.widget.ImageView;
    import android.widget.ViewSwitcher;

    public class MainActivity extends Activity implements
    AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    final Handler mHandler = new Handler();
    int delay = 1000;
    int pause = 5000;
    Timer timer = new Timer();
    timer.schedule(new TimerTask(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            int position = 0;
            mSwitcher.setImageResource(mImageIds[position]);
            position++;
        }

    }, delay, pause);
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    setContentView(R.layout.activity_main);

    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView parent, View v, int position, long id) {
    mSwitcher.setImageResource(mImageIds[position]);
    }

    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return i;
    }

    private ImageSwitcher mSwitcher;

    public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(R.drawable.ic_launcher);
        return i;
        }

        private Context mContext;

        }

        private Integer[] mThumbIds = { R.drawable.image1, R.drawable.image2,            R.drawable.image3, R.drawable.image4, R.drawable.image5 };
enter code here
private Integer[] mImageIds = { R.drawable.image1,
        R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5};

}

XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageSwitcher
    android:id="@+id/switcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

    <Gallery
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#55000000"
    android:gravity="center_vertical"
    android:spacing="16dp" />

    </RelativeLayout>

日志猫:

     06-17 12:13:28.418: D/dalvikvm(357): GC_EXTERNAL_ALLOC freed 910 objects / 62408          bytes in 46ms
     06-17 12:13:29.428: D/dalvikvm(357): GC_EXTERNAL_ALLOC freed 268 objects / 12480 bytes in 38ms
     06-17 12:13:29.758: W/dalvikvm(357): threadid=7: thread exiting with uncaught   exception (group=0x4001d800)
     06-17 12:13:29.758: E/AndroidRuntime(357): FATAL EXCEPTION: Timer-0
     06-17 12:13:29.758: E/AndroidRuntime(357):   android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created   a view hierarchy can touch its views.
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.ViewRoot.focusableViewAvailable(ViewRoot.java:1596)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:451)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:451)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:451)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:451)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.View.setFlags(View.java:4493)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.view.View.setVisibility(View.java:3030)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.widget.ViewAnimator.showOnly(ViewAnimator.java:146)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.widget.ViewAnimator.setDisplayedChild(ViewAnimator.java:102)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at  android.widget.ViewAnimator.showNext(ViewAnimator.java:120)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at android.widget.ImageSwitcher.setImageResource(ImageSwitcher.java:42)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at  com.example.slideshow.MainActivity$1.run(MainActivity.java:37)
     06-17 12:13:29.758: E/AndroidRuntime(357):     at   java.util.Timer$TimerImpl.run(Timer.java:289)
     06-17 12:13:46.839: I/Process(357): Sending signal. PID: 357 SIG: 9
4

2 回答 2

1

这是因为您试图从错误的线程访问视图,这里

  @Override
        public void run() {
            // TODO Auto-generated method stub
            int position = 0;
            mSwitcher.setImageResource(mImageIds[position]);
            position++;
        } 

runOnUiThread改为使用

 @Override
            public void run() {
                // TODO Auto-generated method stub
                int position = 0;
           runOnUiThread(new Runnable() {

           @Override
           public void run() {
              mSwitcher.setImageResource(mImageIds[position]);
            }
          });

            position++;
            } 
于 2013-06-17T07:01:50.080 回答
1

您应该仅从主线程使用 UI。

另外,请注意您在每个计时器滴答声中重新初始化位置变量。

请执行下列操作:

1) 将职位声明为活动变量

2)使用以下代码:

timer.schedule(new TimerTask(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {

            @Override
            public void run() {
                mSwitcher.setImageResource(mImageIds[position]);
            }
        });
            position++;
        }

    }, delay, pause);
于 2013-06-17T07:02:55.803 回答