簡體   English   中英

單擊按鈕時應用程序崩潰

[英]Application crashes when button clicked

這是我正在制作的動畫的演示應用程序。 這是一個帶有簡單圖像的應用程序,它應該對按鈕單擊做出反應,如旋轉、放大、縮小、淡入/淡出等,但它卻崩潰了,我不可能找到問題所在,我已經搜索過互聯網上的解決方案,但沒有運氣,這里是代碼:

public class MainActivity extends AppCompatActivity implements
        View.OnClickListener, Animation.AnimationListener {

    @Override
    public void onAnimationEnd(Animation animation) {
        textStatus.setText("STOPPED");
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public void onAnimationStart(Animation animation) {
        textStatus.setText("RUNNING");
    }

    Animation animFadeIn;
    Animation animFadeOut;
    Animation animFadeInOut;
    Animation animZoomIn;
    Animation animZoomOut;
    Animation animLeftRight;
    Animation animRightLeft;
    Animation animTopBottom;
    Animation animBounce;
    Animation animFlash;
    Animation animRotateLeft;
    Animation animRotateRight;

    ImageView imageView;
    TextView textStatus;

    Button btnFadeIn;
    Button btnFadeOut;
    Button btnFadeInOut;
    Button zoomIn;
    Button zoomOut;
    Button leftRight;
    Button rightLeft;
    Button topBottom;
    Button bounce;
    Button flash;
    Button rotateLeft;
    Button rotateRight;
    SeekBar seekBarSpeed;
    TextView textSeekerSpeed;

    int seekSpeedProgress;

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

        loadAnimations();
        loadUI();
    }

    private void loadAnimations() {
        animFadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in);
        animFadeIn.setAnimationListener(this);
        animFadeOut = AnimationUtils.loadAnimation(this,R.anim.fade_out);
        animFadeInOut = AnimationUtils.loadAnimation(this, R.anim.fade_in_out);

        animZoomIn = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
        animZoomOut = AnimationUtils.loadAnimation(this,R.anim.zoom_out);

        animLeftRight = AnimationUtils.loadAnimation(this,R.anim.left_right);
        animRightLeft = AnimationUtils.loadAnimation(this, R.anim.right_left);

        animTopBottom = AnimationUtils.loadAnimation(this, R.anim.top_bot);
        animBounce = AnimationUtils.loadAnimation(this,R.anim.bounce);
        animFlash = AnimationUtils.loadAnimation(this, R.anim.flash);

        animRotateLeft = AnimationUtils.loadAnimation(this, R.anim.rotate_left);
        animRotateRight = AnimationUtils.loadAnimation(this, R.anim.rotate_right);
    }

    private void loadUI() {
        imageView = (ImageView) findViewById(R.id.imageView);
        textSeekerSpeed = (TextView) findViewById(R.id.textStatus);

        btnFadeIn = (Button) findViewById(R.id.btnFadeIn);
        btnFadeOut = (Button) findViewById(R.id.btnFadeOut);
        btnFadeInOut = (Button) findViewById(R.id.btnFadeInOut);

        zoomIn = (Button) findViewById(R.id.btnZoomIn);
        zoomOut = (Button) findViewById(R.id.btnZoomOut);
        leftRight = (Button) findViewById(R.id.btnLeftRight);
        rightLeft = (Button) findViewById(R.id.btnRightLeft);
        topBottom = (Button) findViewById(R.id.btnTopBottom);
        bounce = (Button) findViewById(R.id.btnBounce);
        flash = (Button) findViewById(R.id.btnFlash);
        rotateLeft = (Button) findViewById(R.id.btnRotateLeft);
        rotateRight = (Button) findViewById(R.id.btnRotateRight);

        zoomIn.setOnClickListener(this);
        zoomOut.setOnClickListener(this);
        leftRight.setOnClickListener(this);
        rightLeft.setOnClickListener(this);
        topBottom.setOnClickListener(this);
        bounce.setOnClickListener(this);
        flash.setOnClickListener(this);
        rotateLeft.setOnClickListener(this);
        rotateRight.setOnClickListener(this);

        seekBarSpeed = (SeekBar) findViewById(R.id.seekBarSpeed);
        textSeekerSpeed = (TextView) findViewById(R.id.textSeekerSpeed);

        seekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
                seekSpeedProgress = value;
                textSeekerSpeed.setText("" +seekSpeedProgress + "of" +seekBarSpeed.getMax());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }



    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btnFadeIn:
                animFadeIn.setDuration(seekSpeedProgress);
                animFadeIn.setAnimationListener(this);
                imageView.startAnimation(animFadeIn);

                break;

            case R.id.btnFadeOut:
                animFadeOut.setDuration(seekSpeedProgress);
                animFadeOut.setAnimationListener(this);
                imageView.startAnimation(animFadeOut);

                break;

            case R.id.btnFadeInOut:
                animFadeInOut.setDuration(seekSpeedProgress);
                animFadeInOut.setAnimationListener(this);
                imageView.startAnimation(animFadeInOut);
                break;

            case R.id.btnZoomIn:
                animZoomIn.setDuration(seekSpeedProgress);
                animZoomIn.setAnimationListener(this);
                imageView.startAnimation(animZoomIn);
                break;

            case R.id.btnZoomOut:
                animZoomOut.setDuration(seekSpeedProgress);
                animZoomOut.setAnimationListener(this);
                imageView.startAnimation(animZoomOut);
                break;

            case R.id.btnLeftRight:
                animLeftRight.setDuration(seekSpeedProgress);
                animLeftRight.setAnimationListener(this);
                imageView.startAnimation(animLeftRight);
                break;

            case R.id.btnRightLeft:
                animRightLeft.setDuration(seekSpeedProgress);
                animRightLeft.setAnimationListener(this);
                imageView.startAnimation(animRightLeft);
                break;

            case R.id.btnBounce:
                animBounce.setDuration(seekSpeedProgress/10);
                animBounce.setAnimationListener(this);
                imageView.startAnimation(animBounce);
                break;

            case R.id.btnFlash:
                animFlash.setDuration(seekSpeedProgress/10);
                animFlash.setAnimationListener(this);
                imageView.startAnimation(animFlash);
                break;

            case R.id.btnRotateLeft:
                animRotateLeft.setDuration(seekSpeedProgress);
                animRotateLeft.setAnimationListener(this);
                imageView.startAnimation(animRotateLeft);
                break;

            case R.id.btnRotateRight:
                animRotateRight.setDuration(seekSpeedProgress);
                animRotateRight.setAnimationListener(this);
                imageView.startAnimation(animRotateRight);
                break;
        }
    }

}

這是崩潰日志:

2020-12-09 22:48:57.075 8696-8696/com.example.animationdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.animationdemo, PID: 8696
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.animationdemo.MainActivity.onAnimationStart(MainActivity.java:31)
        at android.view.animation.Animation.dispatchAnimationStart(Animation.java:999)
        at android.view.animation.AnimationSet.getTransformation(AnimationSet.java:392)
        at android.view.animation.Animation.getTransformation(Animation.java:1030)
        at android.view.View.applyLegacyAnimation(View.java:21166)
        at android.view.View.draw(View.java:21288)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
        at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:1975)
        at android.view.View.updateDisplayListIfDirty(View.java:20474)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
        at android.view.View.updateDisplayListIfDirty(View.java:20441)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
        at android.view.View.updateDisplayListIfDirty(View.java:20441)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
        at android.view.View.updateDisplayListIfDirty(View.java:20441)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
        at android.view.View.updateDisplayListIfDirty(View.java:20441)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4428)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4401)
        at android.view.View.updateDisplayListIfDirty(View.java:20441)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:584)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:590)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:668)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:3840)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3648)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2954)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1857)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8089)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
        at android.view.Choreographer.doCallbacks(Choreographer.java:875)
        at android.view.Choreographer.doFrame(Choreographer.java:776)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

問題是您在未初始化時嘗試訪問 textStatus 視圖的 setText 方法。

從您的代碼中,您沒有在任何地方對其進行初始化。 我相信

textSeekerSpeed = (TextView) findViewById(R.id.textStatus);

應該是你初始化 textStatus 的地方。

嘗試更換

textSeekerSpeed = (TextView) findViewById(R.id.textStatus);

textStatus = (TextView) findViewById(R.id.textStatus);

然后用相應的 TextView 設置textSeekerSpeed 它應該可以解決您的問題

改變

textSeekerSpeed = (TextView) findViewById(R.id.textStatus);

textStatus = (TextView) findViewById(R.id.textStatus);

textStatus從未被映射,因此它始終為 null 並導致崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM