【问题标题】:Why does my android app keep stopping in the Android Studio emulator?为什么我的 android 应用程序在 Android Studio 模拟器中不断停止?
【发布时间】:2019-11-28 22:36:55
【问题描述】:

我尝试修复所有问题,但似乎找不到问题所在。这是我的 MainActivity.java 文件:

package com.weebly.chesslearners.firstgame;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.renderscript.Sampler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.view.animation.TranslateAnimation;
import static android.content.ContentValues.TAG;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new GamePanel(this));

    final TextView player = (TextView) findViewById(R.id.textView);
    final Button mover = (Button) findViewById(R.id.btnmove);

    final ObjectAnimator animation = ObjectAnimator.ofFloat(player,"y",300, 500);
    mover.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            animation.setDuration(2000);
            animation.setRepeatCount(1);
            animation.setRepeatMode(ValueAnimator.REVERSE);
            animation.start();
        }
    });



    setContentView(R.layout.activity_main);
    Log.i(TAG, "Build Successful");

}

}

我也添加了所有的导入。 构建输出显示错误位于“mover.setOnclick ...”行,但我找不到导致错误的原因。

【问题讨论】:

  • 您能否通过查看logcat 添加一些日志。这将有助于了解确切的问题。
  • 点击“移动器”时应用程序是否停止?你可能有一些内存泄漏。添加logcat 日志将有助于发现您的问题

标签: java android nullpointerexception objectanimator


【解决方案1】:

我认为问题出在setContentView(new GamePanel(this));这一行

一旦你设置了视图,编译器就会尝试在 GamePanel() 类中找到你的 textViews 和按钮,我猜它不存在。

您可以通过添加来解决它 setContentView(R.layout.activity_main);final TextView player = (TextView) findViewById(R.id.textView);

稍后设置动态 UI 组件。

【讨论】:

    猜你喜欢
    • 2014-09-22
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2021-09-26
    • 2017-02-10
    相关资源
    最近更新 更多