简体   繁体   English

为什么我的 Android 应用程序在 Android Studio 模拟器中一直停止?

[英]Why does my android app keep stopping in the Android Studio emulator?

I tried fixing everything, but I can't seem to find the problem.我尝试修复所有问题,但似乎找不到问题所在。 Here is my MainActivity.java file:这是我的 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");

}

} }

I added all of the imports too.我也添加了所有进口产品。 The Build Output says the error is on the line "mover.setOnclick...", but I can't find whats causing the error.生成输出显示错误在“mover.setOnclick ...”行中,但我找不到导致错误的原因。

I think the problem is in the line setContentView(new GamePanel(this));我认为问题在于setContentView(new GamePanel(this));

Once you set the view the compiler is trying to find your textViews and button in GamePanel() class and I guess its not there.一旦你设置了视图,编译器就会试图在 GamePanel() 类中找到你的 textViews 和按钮,我猜它不在那里。

You can resolve it by adding setContentView(R.layout.activity_main);您可以通过添加setContentView(R.layout.activity_main);来解决它before final TextView player = (TextView) findViewById(R.id.textView);final TextView player = (TextView) findViewById(R.id.textView);

and set dynamic UI component later.稍后设置动态 UI 组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM