gpt4 book ai didi

java - BitmapDrawable 无法转换为 AnimationDrawable

转载 作者:行者123 更新时间:2023-12-01 16:56:28 26 4
gpt4 key购买 nike

我正在开发一个应用程序,一旦我摇动手机,它就会执行某项功能。为此,我创建了一个名为 ShakeListener 的类。当我打开它时,我的应用程序运行得非常好。当我打开应用程序并按后退按钮并转到主屏幕并摇动手机时,它会打开闪光灯(就像预期的那样),但当我再次打开应用程序时,它崩溃了。我不知道为什么会这样

我的主要 Activity 是

public class MainActivity extends ActionBarActivity   {
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
SharedPreferences getprefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean stopshake = getprefs.getBoolean("checkbox", true);
if (stopshake) {
mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
public void onShake() {
if (!isFlashOn && mShaker != null ) {

getCamera();
turnOnFlash();
} else {
turnOffFlash();

}
}
});
} else {
if (mShaker != null) {
mShaker.setOnShakeListener(null);
mShaker = null;
}
}
}

private ToggleButton togle;
private Camera camera;
private boolean isFlashOn;

private boolean hasFlash;
Parameters params;
private ShakeListener mShaker;
MediaPlayer mp;
ImageView anime;
int p=1;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

anime = (ImageView) findViewById(R.id.Animation);

hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish(); }
});
alert.show();
return;}


getCamera();


togle = (ToggleButton) findViewById(R.id.ToggleButton01);

togle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean checked = ((ToggleButton) v).isChecked();
if (checked){
turnOffFlash();
}

else{
getCamera();
turnOnFlash();
}
}
});


}

private void getCamera() {
// TODO Auto-generated method stub
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
} }

private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
getCamera();
playSound();

params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
anime.setImageResource(R.drawable.anim);
anime.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation =
(AnimationDrawable) anime.getDrawable();
frameAnimation.start();
}
});

// changing button/switch image
}
}

private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();

params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
isFlashOn = false;
anime.setImageResource(R.drawable.off);
// changing button/switch image
}
}

private void playSound() {
// TODO Auto-generated method stub
if(isFlashOn){
mp = MediaPlayer.create(MainActivity.this, R.raw.off1);
}else{
mp = MediaPlayer.create(MainActivity.this, R.raw.on1);
}
mp.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}

@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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.action_settings:
Intent intent = new Intent(MainActivity.this, Prefsetting.class);
startActivity(intent);
break;

case R.id.about:
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("This is an alert with no consequence");
dlgAlert.setTitle("App Title");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
break;

}
return true;


}
}

我的 logcat 显示

08-23 04:31:53.205: D/ShakeListener(11990): ShakeListener invoked----> 08-23 04:31:53.207: D/ShakeListener(11990): ShakeListener setOnShakeListener invoked----> 08-23 04:31:53.312: D/AndroidRuntime(11990): Shutting down VM 08-23 04:31:53.314: E/AndroidRuntime(11990): FATAL EXCEPTION: main 08-23 04:31:53.314: E/AndroidRuntime(11990): Process: com.shakylight, PID: 11990 08-23 04:31:53.314: E/AndroidRuntime(11990): java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable 08-23 04:31:53.314: E/AndroidRuntime(11990): at com.shakylight.MainActivity$4.run(MainActivity.java:145) 08-23 04:31:53.314: E/AndroidRuntime(11990): at android.os.Handler.handleCallback(Handler.java:739) 08-23 04:31:53.314: E/AndroidRuntime(11990): at android.os.Handler.dispatchMessage(Handler.java:95) 08-23 04:31:53.314: E/AndroidRuntime(11990): at android.os.Looper.loop(Looper.java:135) 08-23 04:31:53.314: E/AndroidRuntime(11990): at android.app.ActivityThread.main(ActivityThread.java:5254) 08-23 04:31:53.314: E/AndroidRuntime(11990): at java.lang.reflect.Method.invoke(Native Method) 08-23 04:31:53.314: E/AndroidRuntime(11990): at java.lang.reflect.Method.invoke(Method.java:372) 08-23 04:31:53.314: E/AndroidRuntime(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 08-23 04:31:53.314: E/AndroidRuntime(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

当您转换时,问题似乎出现在以下行:

AnimationDrawable frameAnimation = (AnimationDrawable) anime.getDrawable(); 

您正在 ImageView 上使用 anime.setImageResource() 来设置您的可绘制对象。请尝试使用 anime.setImageDrawable(getResource().getDrawable(R.drawable.anim); 并查看错误是否仍然存在。

关于java - BitmapDrawable 无法转换为 AnimationDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com