我想做一个动画,我将图像视图从下到上移动。到目前为止我有这个代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ImageView bubbles = (ImageView) findViewById(R.id.bubbles);
//bubbles.setVisibility(View.GONE);
Animation animContentUp = AnimationUtils.loadAnimation(this, R.anim.slide_up_service);
animContentUp.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
bubbles.startAnimation(animContentUp); // line 48
}
slide_up_service.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="700"
android:fromXDelta="0%p"
android:toXDelta="0%p"
android:fromYDelta="0%"
android:toYDelta="120%">
</translate>
但是当我打电话给bubbles.startAnimation(animContentUp);
时,它会崩溃。
有谁知道问题是什么?
此外,xml来自一个帖子,询问如何从上到下移动,我怎样才能从头到尾?
由于
05-31 16:49:31.035: E/AndroidRuntime(3026): FATAL EXCEPTION: main
05-31 16:49:31.035: E/AndroidRuntime(3026): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.arin/android.arin.LoginScreen}: java.lang.NullPointerException
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.ActivityThread.access$700(ActivityThread.java:143)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.os.Looper.loop(Looper.java:137)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.ActivityThread.main(ActivityThread.java:4950)
05-31 16:49:31.035: E/AndroidRuntime(3026): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 16:49:31.035: E/AndroidRuntime(3026): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 16:49:31.035: E/AndroidRuntime(3026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-31 16:49:31.035: E/AndroidRuntime(3026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-31 16:49:31.035: E/AndroidRuntime(3026): at dalvik.system.NativeStart.main(Native Method)
05-31 16:49:31.035: E/AndroidRuntime(3026): Caused by: java.lang.NullPointerException
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.arin.LoginScreen.onCreate(LoginScreen.java:48)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.Activity.performCreate(Activity.java:5177)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-31 16:49:31.035: E/AndroidRuntime(3026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-31 16:49:31.035: E/AndroidRuntime(3026): ... 11 more
答案 0 :(得分:0)
NullPointerException
可能是指气泡,因此findViewById()
找不到您的ImageView。你的身份证了吗?
答案 1 :(得分:0)
您无法使用当前布局中不存在的视图。findViewById(R.id.bubbles)
这将返回null,因为它位于fragment_loginscreen
中,而不是您用作activity_loginscreen
的{{1}}活动中的布局
<强>溶液强>
将您的图片视图放入activity_loginscreen
版面,或者只是将活动的布局更改为fragment_loginscreen
答案 2 :(得分:0)
bubbles
为空,因为在View
布局中没有@id/bubbles
代码为activity_login_screen
的内容。