gpt4 book ai didi

android - NullPointerException导致android动画崩溃

转载 作者:行者123 更新时间:2023-12-02 14:33:44 27 4
gpt4 key购买 nike

我一直在尝试制作动画,但是由于某种我无法理解的原因而崩溃。
在日志猫中它写是因为nullpointerexception。但是我不明白
我该怎么办\更改。
我怎样才能解决这个问题?谢谢!

主要 Activity :

package nir.rauch.again;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Animation anim = AnimationUtils.loadAnimation(getBaseContext(),
R.anim.correctanim);
ImageView duck = (ImageView) findViewById(R.drawable.duckwithswaves);
duck.startAnimation(anim);
}

@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;
}

}

它的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:contentDescription="Duck Animation"
android:src="@drawable/duckwithswaves" />

</RelativeLayout>

动画xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >


<rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/cycle_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="3800"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />

</set>

LOGCAT:
04-14 15:19:23.068: E/AndroidRuntime(21445): FATAL EXCEPTION: main
04-14 15:19:23.068: E/AndroidRuntime(21445): java.lang.RuntimeException: Unable to start activity ComponentInfo{nir.rauch.again/nir.rauch.again.MainActivity}: java.lang.NullPointerException
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.os.Looper.loop(Looper.java:137)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-14 15:19:23.068: E/AndroidRuntime(21445): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 15:19:23.068: E/AndroidRuntime(21445): at java.lang.reflect.Method.invoke(Method.java:525)
04-14 15:19:23.068: E/AndroidRuntime(21445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-14 15:19:23.068: E/AndroidRuntime(21445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-14 15:19:23.068: E/AndroidRuntime(21445): at dalvik.system.NativeStart.main(Native Method)
04-14 15:19:23.068: E/AndroidRuntime(21445): Caused by: java.lang.NullPointerException
04-14 15:19:23.068: E/AndroidRuntime(21445): at nir.rauch.again.MainActivity.onCreate(MainActivity.java:19)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.Activity.performCreate(Activity.java:5372)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-14 15:19:23.068: E/AndroidRuntime(21445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-14 15:19:23.068: E/AndroidRuntime(21445): ... 11 more

最佳答案

尝试这个..
findViewById应该包含ImageView的ID

改变这个

ImageView duck = (ImageView) findViewById(R.drawable.duckwithswaves);


ImageView duck = (ImageView) findViewById(R.id.idofimageview);

关于android - NullPointerException导致android动画崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060045/

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