这里的总菜鸟,请原谅我的无知。我找到了一些动画ImageView的代码:
public void startAnimatedBackground(Integer num) {
ImageSwitcher imageSwitcher = null;
switch (num) {
case 0:
imageSwitcher = img_0;
break;
case 1:
imageSwitcher = img_1;
break;
case 2:
imageSwitcher = img_2;
break;
case 3:
imageSwitcher = img_3;
break;
default:
break;
}
imageSwitcher.setInAnimation(aniIn);
imageSwitcher.setOutAnimation(aniOut);
imageSwitcher.setFactory(this);
imageSwitcher.setImageResource(images[index]);
final Handler handler = new Handler();
final ImageSwitcher finalImageSwitcher = imageSwitcher;
Runnable runnable = new Runnable() {
@Override
public void run() {
if (isRunning) {
index++;
index = index % images.length;
finalImageSwitcher.setImageResource(images[index]);
handler.postDelayed(this, interval);
}
}
};
handler.postDelayed(runnable, interval);
}
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
return imageView;
}
@Override
public void finish() {
isRunning = false;
super.finish();
}}
我在onCreate()上创建了一些所需的代码:
private int index = 0;
private boolean isRunning = true;
Animation aniIn;
Animation aniOut;
ImageSwitcher img_0,img_1,img_2,img_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_walk_in_progress);
aniIn = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
aniOut = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
aniIn.setDuration(500);
aniOut.setDuration(500);
img_0 = (ImageSwitcher) findViewById(R.id.switcher_accept);
img_1 = (ImageSwitcher) findViewById(R.id.switcher_on_way);
img_2 = (ImageSwitcher) findViewById(R.id.switcher_in_progress);
img_3 = (ImageSwitcher) findViewById(R.id.switcher_finished);
startAnimatedBackground(0);
}
现在第一个工作正常。现在在一个Activity中,我试图将其切换为第二个图像开始动画:
编辑:这个新的“活动”不是一个让视图膨胀的活动。新活动是GCM Intent Service,它只是监听通知并更新我的通知信息。当我收到此通知时,我想获取当前在用户屏幕上显示的视图并更新动画以将其切换到第二个图像。
WalkInProgress walk = new WalkInProgress();
walk.startAnimatedBackground(1);
这会导致错误:
02-01 16:03:04.766 23805-24580 / ly.leash.Leashly I / System.out:null 02-01 16:03:04.778 23805-24580 / ly.leash.Leashly E / AndroidRuntime:FATAL EXCEPTION:IntentService [GcmIntentService] 过程:ly.leash.Leashly,PID:23805 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageSwitcher.setInAnimation(android.view.animation.Animation)' at ly.leash.Leashly.WalkInProgress.startAnimatedBackground(WalkInProgress.java:179) at ly.leash.Leashly.GcmIntentService.sendNotification(GcmIntentService.java:96) at ly.leash.Leashly.GcmIntentService.onHandleIntent(GcmIntentService.java:72) 在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.os.HandlerThread.run(HandlerThread.java:61)
因此无论出于何种原因,动画都会返回null。
我已尝试在startAnimatedBackground函数中声明它而不是onCreate,但这也会导致错误:
02-01 16:15:10.488 25553-25930 / ly.leash.Leashly E / AndroidRuntime:FATAL EXCEPTION:IntentService [GcmIntentService] 过程:ly.leash.Leashly,PID:25553 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.content.res.Resources android.content.Context.getResources()' 在android.content.ContextWrapper.getResources(ContextWrapper.java:85) 在android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74) 在android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:73) at ly.leash.Leashly.WalkInProgress.startAnimatedBackground(WalkInProgress.java:152) at ly.leash.Leashly.GcmIntentService.sendNotification(GcmIntentService.java:96) at ly.leash.Leashly.GcmIntentService.onHandleIntent(GcmIntentService.java:72) 在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.os.HandlerThread.run(HandlerThread.java:61)
错误就在这一行:
aniIn = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
答案 0 :(得分:1)
空指针的原因是您尝试访问的ImageSwitcher
处于不同的活动中。您必须使用img_1 = (ImageSwitcher) findViewById(R.id.switcher_on_way);
在此新活动中再次实例化它(假设ImageSwitcher
窗口小部件switcher_on_way
位于新活动的XML布局文件中。)
<强>更新强>
为了从GCM Intent Service向您当前正在运行的活动发送消息,您需要向活动发送广播。
确认用户当前正在查看您的活动后,请创建Intent
并向活动发送广播:
/* create intent */
Intent intent = new Intent("my_animation");
intent.putExtra("message", message); <-- replace this with what you
want to broadcast to your
activity (which would be an
integer specifying the new
ImageSwitcher)
/* send broadcast */
this.sendBroadcast(intent);
在您的活动中,创建一个处理广播意图的BroadcastReceiver
。
/* this handler will process the broadcast intent */
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
// you could start your animation here
}
};
在BroadcastReceiver
中注册onResume
并在onPause
中取消注册。
@Override
protected void onResume() {
super.onResume();
this.registerReceiver(messageReceiver, new IntentFilter("my_animation"));
}
@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(messageReceiver);
}
我希望这会有所帮助。