0

ボタンクリックで画像を切り替えようとしましたが、失敗してエラーが発生しました。コードは次のとおりです。誰か助けてください。必要に応じてXMLコードを追加してくださいまた同じものを投稿してください

package com.conn;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;

public class image_slider extends Activity
{

        Integer[] imageIDs = { R.drawable.haha,  R.drawable.dte,R.drawable.new_login };
        private ImageSwitcher imageSwitcher;
        private Button nextButton;
        private Button previousButton; 

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.imgslide);
            imageSwitcher.setImageResource(imageIDs[0]);
            nextButton = (Button) findViewById(R.id.next);
            nextButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    final Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);       
                    final Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);     
                    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);      
                    imageSwitcher.setFactory(this);   
                    imageSwitcher.setInAnimation(in);      
                    imageSwitcher.setOutAnimation(out);             
                    imageSwitcher.setImageResource(imageIDs[1]);
//                  imageSwitcher.setImageResource(imageIDs[1]);

                }

            }); 

            previousButton = (Button) findViewById(R.id.previous);
            previousButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    final Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_left);
                    final Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_right);
                    imageSwitcher.setFactory(this);
                    imageSwitcher.setInAnimation(in);
                    imageSwitcher.setOutAnimation(out);
                    imageSwitcher.setImageResource(imageIDs[0]);

                }

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

}
4

1 に答える 1

1

あなたの問題は、あなたがあなたの中で言及するときthisOnClickListenerあなたの現在の活動ではなく、その特定のリスナーを参照しているということです。次のように変更する必要があります

 final Animation out= AnimationUtils.loadAnimation(image_slider.this, android.R.anim.slide_out_right); 
 final Animation in= AnimationUtils.loadAnimation(image_slider.this, android.R.anim.slide_in_left);
 ...
 imageSwitcher.setFactory(image_slider.this);

また、 slide_out_leftと "slide_in_right"は存在しないため、これらは使用android.R.anim.slide_in_leftしないでください。android.R.anim.slide_out_right

ところで、Javaの良い習慣は、クラス名を大文字で始めることです。ImageSlider

于 2011-06-20T12:32:50.117 に答える