0

我有图像幻灯片应用程序,我试图让用户在设置活动时从许多动画中进行选择,所以我使用 SharedPreferences 来实现这一点,当我运行应用程序时它打开但它只显示第一张图像并快速退出应用程序而无需强制关闭,我是新手android开发,我认为我的代码中缺少一些东西,

所以请任何帮助来解决这个问题,我们将不胜感激,谢谢。

MainActivity.java

 public class MainActivity extends Activity {

public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;

private int[] IMAGE_IDS = {
        R.drawable.one, R.drawable.two, R.drawable.three,R.drawable.four,R.drawable.five,
        R.drawable.six,R.drawable.seven, R.drawable.eight, R.drawable.nine,

        };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();              
        }
    };

    int delay = 1000; // delay for 1 sec.

    int period = 8000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler.post(mUpdateResults);
    }

    }, delay, period);             
}

public void onClick(View v) {

    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
  }

private void AnimateandSlideShow() {

SharedPreferences getPrefs = PreferenceManager
        .getDefaultSharedPreferences(getBaseContext());

boolean animation_two = getPrefs.getBoolean("animation_two", true);             
boolean animation = getPrefs.getBoolean("animation", false);
boolean animation_one = getPrefs.getBoolean("animation_one", false);        

if (animation_two == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

    currentimageindex++;

    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);

      slidingimage.startAnimation(rotateimage);  
      finish();
}else if(animation_one == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

    currentimageindex++;

    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);

      slidingimage.startAnimation(rotateimage);  

                finish();

}else if (animation == true) {
     slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
        slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

        currentimageindex++;

        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in2);

          slidingimage.startAnimation(rotateimage);  
                finish();

    } else if(animation_two == false && animation == false && animation_one == false){
    slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

    currentimageindex++;

    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in2);

  slidingimage.startAnimation(rotateimage);  
  finish();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {

    case R.id.action_settings:
        Intent p = new Intent("com.test.demo.SETTING");
        startActivity(p);
    break;

        }
return false; 
}
}
4

1 回答 1

0

重新排列您的代码,它将正常工作,如下所示:

 public class MainActivity extends Activity {

public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;

private int[] IMAGE_IDS = {
    R.drawable.one, R.drawable.two, R.drawable.three,R.drawable.four,R.drawable.five,
    R.drawable.six,R.drawable.seven, R.drawable.eight, R.drawable.nine,

    };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Handler mHandler = new Handler();
 // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();              
        }
    };

    int delay = 1000; // delay for 1 sec.

    int period = 8000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler.post(mUpdateResults);
    }

    }, delay, period);             

}      

private void AnimateandSlideShow() {

    SharedPreferences getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    boolean animation_two = getPrefs.getBoolean("animation_two", true);             
            boolean animation = getPrefs.getBoolean("animation", false);
            boolean animation_one = getPrefs.getBoolean("animation_one", false);

 if (animation == true) {   
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);               
      slidingimage.startAnimation(rotateimage);   

}else if(animation_one == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;        
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);        
      slidingimage.startAnimation(rotateimage);   

}else if (animation_two == true) {
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in2);       
      slidingimage.startAnimation(rotateimage);  

}else if(animation == false && animation_one == false && animation_two == false){
    slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
    currentimageindex++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in2);        
      slidingimage.startAnimation(rotateimage); 
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
  }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {

case R.id.action_settings:
    Intent p = new Intent("com.test.demo.SETTING");
    startActivity(p);
break;

    }
return false; 
}
}

希望能帮到你

于 2013-08-17T00:05:50.830 回答