模拟器未在Android Studio中显示活动

时间:2019-01-02 04:51:31

标签: android-studio android-emulator

当我为我的应用运行模拟器时,该模拟器会运行,并且android studio不会给我任何错误。 android studio中没有高亮显示错误。但是,模拟器运行时不会加载我的活动。我尝试运行的应用程序打开,但只显示白屏。

我在Java文件上运行了调试器,没关系。我无法浏览文件,因为无法加载活动,因此无法与应用交互。

这是java文件:

public class MainActivity extends AppCompatActivity {
boolean check = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button green = (Button) findViewById(R.id.Green);
    Button blue = (Button) findViewById(R.id.Blue);
    Button red =  (Button) findViewById(R.id.Red);
    Button purple = (Button) findViewById(R.id.purple);
    TextView Final = (TextView) findViewById(R.id.Score);
    int score = 0;
    do {
        int[] pattern = new int[score + 1];
        for (int i = 0;i<score + 1;i++) {
            Random rand = new Random();
            pattern[i] = rand.nextInt(4);

            switch (pattern[i]) {
                case 0:
                    green.setBackgroundColor(getColor(R.color.lightGreen));
                    SystemClock.sleep(1000);
                case 1:
                    blue.setBackgroundColor(getColor(R.color.lightBlue));
                    SystemClock.sleep(1000);
                case 2:
                    purple.setBackgroundColor(getColor(R.color.lightPurple));
                    SystemClock.sleep(1000);
                case 3:
                    red.setBackgroundColor(getColor(R.color.lightRed));
                    SystemClock.sleep(1000);
            }

        }

        final int[] clicks = {0};
        final int[] guess= new int[score + 1];


        while (clicks[0] < pattern.length) {
            green.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    guess[clicks[0]] = 0;
                    clicks[0]++;
                }
            });
            blue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    guess[clicks[0]] = 1;
                    clicks[0]++;
                }
            });

            purple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    guess[clicks[0]] = 2;
                    clicks[0]++;
                }
            });

            red.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    guess[clicks[0]] = 3;
                    clicks[0]++;
                }
            });

        }

        if (!Arrays.equals(pattern,guess)) {
            check = false;
            score++;
        }

    }while (check);

    String mark = "Your score is " + score;
    Final.setText(mark);

}

}

这是活动文件的顶部和底部:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/View"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

[Code for other buttons/textviews]

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案