Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文本视图';s字符串设置错误-如何修复它?_Java_Android - Fatal编程技术网

Java 文本视图';s字符串设置错误-如何修复它?

Java 文本视图';s字符串设置错误-如何修复它?,java,android,Java,Android,我在安卓工作室工作了大约五周。在这段时间里,我学习了如何设置TextView的字符串并设置包含该字符串的字符串变量。我一直在尝试为android设备制作一款应用程序游戏,在更改一些代码时遇到了一个问题 textView myText应该指定要按下的彩色按钮,但当我在android emulator中按下开始按钮时,随机生成的文本和按钮永远不会相互关联,即使我有一系列的if语句来确保它们相互关联 出于某种原因,当我运行代码时,第一个颜色按钮始终是正确的按钮,然后运行方法颜色开关(如果正确),但我不

我在安卓工作室工作了大约五周。在这段时间里,我学习了如何设置TextView的字符串并设置包含该字符串的字符串变量。我一直在尝试为android设备制作一款应用程序游戏,在更改一些代码时遇到了一个问题

textView myText应该指定要按下的彩色按钮,但当我在android emulator中按下开始按钮时,随机生成的文本和按钮永远不会相互关联,即使我有一系列的if语句来确保它们相互关联

出于某种原因,当我运行代码时,第一个颜色按钮始终是正确的按钮,然后运行方法颜色开关(如果正确),但我不知道将正确的颜色按钮连接到正确的文本有什么问题。哦,我把这个活动和一个失败游戏活动联系起来,当按下任何错误的按钮时就会调用这个活动。如果您想要其他活动的代码,我很乐意展示

以下是我的游戏活动xml文件:

<?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/game_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.a2118667.red.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/indicatorText">

    <Button
        android:id="@+id/startButton"
        android:layout_width="175dp"
        android:layout_height="75dp"
        android:layout_marginTop="8dp"
        android:text="@string/Game_Start_Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.156" />

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="299dp"
        android:layout_height="301dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.88">

        <Button
            android:id="@+id/button1"
            android:layout_width="150dp"
            android:layout_height="150dp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_column="0"
            android:layout_row="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_column="1"
            android:layout_row="0" />

        <Button
            android:id="@+id/button4"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_column="1"
            android:layout_row="1" />
    </GridLayout>

    <TextView
        android:id="@+id/indicatorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text=" "
        android:textAppearance="@style/TextAppearance.AppCompat.Button"
        android:textSize="34sp"
        app:layout_constraintBottom_toTopOf="@+id/startButton"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>

这是我的游戏活动的Java文件

    package com.example.a2118667.red;
//imports
import android.content.Intent;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.widget.TextView;
import java.util.Random;

import static  com.example.a2118667.red.R.id.startButton;

public class MainActivity extends AppCompatActivity {

    final int[] cowbears = {0};
    final int[] cowbears2 = {0};
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView myText = (TextView) findViewById(R.id.indicatorText);

        final Button myButton = (Button) findViewById(R.id.startButton);

        final Button button1 = (Button) findViewById(R.id.button1);
        final Button button2 = (Button) findViewById(R.id.button2);
        final Button button3 = (Button) findViewById(R.id.button3);
        final Button button4 = (Button) findViewById(R.id.button4);


        myButton.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                        ((ViewGroup) myButton.getParent()).removeView(myButton);
                        colourSwitch();
                    }
                }
        );




        button4.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {

                        myText.setText("cowBears rock");
                        // if button says yellow and button is yellow
                        if (cowbears[0] == 2 && cowbears2[0] <= 1 || cowbears[0] == 6 && cowbears2[0] <= 1) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says blue and button blue
                        if (cowbears[0] == 3 && cowbears2[0] == 2 || cowbears[0] == 7 && cowbears2[0] == 2) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says green and button green
                        if (cowbears[0] == 4 && cowbears2[0] == 3 || cowbears[0] == 8 && cowbears2[0] == 3) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says red and button red
                        if (cowbears[0] <= 1 && cowbears2[0] == 4 || cowbears[0] == 5 && cowbears2[0] == 4) {
                            myText.setText("You did it, mommy!");
                        }
                        String verify = myText.getText().toString();
                        if (verify.equals("cowBears rock")){
                            losedThegameBoi();
                        }

                        String check = myText.getText().toString();
                        if (check.equals("You did it, mommy!")) {
                            colourSwitch();
                        }

                    }
                }

        );

        button3.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {

                        myText.setText("cowBears rock");
                        // if button says yellow and button is yellow
                        if (cowbears[0] == 3 && cowbears2[0] <= 1 || cowbears[0] == 8 && cowbears2[0] <= 1) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says blue and button blue
                        if (cowbears[0] == 4 && cowbears2[0] == 2 || cowbears[0] == 5 && cowbears2[0] == 2) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says green and button green
                        if (cowbears[0] <= 1 && cowbears2[0] == 3 || cowbears[0] == 6 && cowbears2[0] == 3) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says red and button red
                        if (cowbears[0] == 2 && cowbears2[0] == 4 || cowbears[0] == 7 && cowbears2[0] == 4) {
                            myText.setText("You did it, mommy!");
                        }
                        String verify = myText.getText().toString();
                        if (verify.equals("cowBears rock")){
                            losedThegameBoi();
                        }

                        String check = myText.getText().toString();
                        if (check.equals("You did it, mommy!")) {
                            colourSwitch();
                        }
                    }
                }
        );


        button2.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {

                        myText.setText("cowBears rock");
                        // if button says yellow and button yellow
                        if (cowbears[0] == 4 && cowbears2[0] <= 1 || cowbears[0] == 7 && cowbears2[0] <= 1) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says blue and button blue
                        if (cowbears[0] <= 1 && cowbears2[0] == 2 || cowbears[0] == 8 && cowbears2[0] == 2) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says green and button green
                        if (cowbears[0] == 2 && cowbears2[0] == 3 || cowbears[0] == 5 && cowbears2[0] == 3) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says red and button red
                        if (cowbears[0] == 3 && cowbears2[0] == 4 || cowbears[0] == 6 && cowbears2[0] == 4) {
                            myText.setText("You did it, mommy!");
                        }
                        String verify = myText.getText().toString();
                        if (verify.equals("cowBears rock")){
                            losedThegameBoi();
                        }

                        String check = myText.getText().toString();
                        if (check.equals("You did it, mommy!")) {
                            colourSwitch();
                        }
                    }
                }
        );


        // onClick Listeners for buttons
        button1.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {

                        //debugged

                        myText.setText("cowBears rock");
                        // if button says yellow and button yellow
                        if (cowbears[0] <= 1 && cowbears2[0] <= 1 || cowbears[0] == 5 && cowbears2[0] <= 1) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says blue and button blue
                        if (cowbears[0] == 2 && cowbears2[0] == 2 || cowbears[0] == 6 && cowbears2[0] == 2) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says green and button green
                        if (cowbears[0] == 3 && cowbears2[0] == 3 || cowbears[0] == 7 && cowbears2[0] == 3) {
                            myText.setText("You did it, mommy!");
                        }
                        // if button says red and button red
                        if (cowbears[0] == 4 && cowbears2[0] == 4 || cowbears[0] == 8 && cowbears2[0] == 4) {
                            myText.setText("You did it, mommy!");
                        }
                        String verify = myText.getText().toString();
                        if (verify.equals("cowBears rock")){
                            losedThegameBoi();

                        }

                        String check = myText.getText().toString();
                        if (check.equals("You did it, mommy!")) {
                            colourSwitch();
                        }

                    }
                }
        );

    }


    private void losedThegameBoi() {
        Intent intent = new Intent(this, Loser_Activity.class);
        startActivity(intent);
    }

    private void colourSwitch() {
        final TextView myText = (TextView) findViewById(R.id.indicatorText);

        final Button button1 = (Button) findViewById(R.id.button1);
        final Button button2 = (Button) findViewById(R.id.button2);
        final Button button3 = (Button) findViewById(R.id.button3);
        final Button button4 = (Button) findViewById(R.id.button4);

        final int[] cowBears = {0};
        final int[] cowBears2 = {0};

        final int[] cowbears = {0};
        final int[] cowbears2 = {0};


        //Random number generator for randomizing colour layout
        Random randomGenerator = new Random();
        //For generating what colour pattern to be displayed
        int randomInt = randomGenerator.nextInt(9);
        myText.setText("Generated : " + randomInt);
        cowBears[0] = randomInt;
        cowbears[0] = randomInt;
        //Colour patterns based on what integer is generated
        //Random number generator for randomizing button click stuff
        Random randomGenerator2 = new Random();
        //For generating which text to be displayed
        int randomInt2 = randomGenerator2.nextInt(5);
        myText.setText("Generated : " + randomInt2);
        cowBears2[0] = randomInt2;
        cowbears2[0] = randomInt2;

        // if statements for the colour scheme
        if (cowBears[0] <= 1) {
            button1.setBackgroundResource(R.drawable.yellow);
            button2.setBackgroundResource(R.drawable.blue);
            button3.setBackgroundResource(R.drawable.green);
            button4.setBackgroundResource(R.drawable.download);
        }

        if (cowBears[0] == 2) {
            button4.setBackgroundResource(R.drawable.yellow);
            button1.setBackgroundResource(R.drawable.blue);
            button2.setBackgroundResource(R.drawable.green);
            button3.setBackgroundResource(R.drawable.download);
        }

        if (cowBears[0] == 3) {
            button3.setBackgroundResource(R.drawable.yellow);
            button4.setBackgroundResource(R.drawable.blue);
            button1.setBackgroundResource(R.drawable.green);
            button2.setBackgroundResource(R.drawable.download);
        }

        if (cowBears[0] == 4) {
            button2.setBackgroundResource(R.drawable.yellow);
            button3.setBackgroundResource(R.drawable.blue);
            button4.setBackgroundResource(R.drawable.green);
            button1.setBackgroundResource(R.drawable.download);
        }
        if (cowBears[0] == 5) {
            button1.setBackgroundResource(R.drawable.yellow);
            button3.setBackgroundResource(R.drawable.blue);
            button2.setBackgroundResource(R.drawable.green);
            button4.setBackgroundResource(R.drawable.download);
        }

        if (cowBears[0] == 6) {
            button4.setBackgroundResource(R.drawable.yellow);
            button1.setBackgroundResource(R.drawable.blue);
            button3.setBackgroundResource(R.drawable.green);
            button2.setBackgroundResource(R.drawable.download);
        }

        if (cowBears[0] == 7) {
            button2.setBackgroundResource(R.drawable.yellow);
            button4.setBackgroundResource(R.drawable.blue);
            button1.setBackgroundResource(R.drawable.green);
            button3.setBackgroundResource(R.drawable.download);
        }

        if (cowBears[0] == 8) {
            button3.setBackgroundResource(R.drawable.yellow);
            button2.setBackgroundResource(R.drawable.blue);
            button4.setBackgroundResource(R.drawable.green);
            button1.setBackgroundResource(R.drawable.download);
        }

        //Here we have the randomizing of the button text based on what integer is genetated for cowbears2
        if (cowBears2[0] <= 1) {
            myText.setText("yellow" + cowBears[0] + cowBears2[0]);
        }

        if (cowBears2[0] == 2) {
            myText.setText("Blue" + cowBears[0] + cowBears2[0]);
        }

        if (cowBears2[0] == 3) {
            myText.setText("Green" + cowBears[0] + cowBears2[0]);
        }

        if (cowBears2[0] == 4) {
            myText.setText("Red" + cowBears[0] + cowBears2[0]);
        }
    }
}
package com.example.a2118667.red;
//进口
导入android.content.Intent;
导入android.support.v4.media.session.MediaSessionCompat;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.view.MotionEvent;
导入android.view.GestureDetector;
导入android.support.v4.view.GestureDetectorCompat;
导入android.widget.TextView;
导入java.util.Random;
导入静态com.example.a2118667.red.R.id.startButton;
公共类MainActivity扩展了AppCompatActivity{
final int[]cowbears={0};
final int[]cowbears2={0};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终文本视图myText=(文本视图)findViewById(R.id.indicatorText);
最终按钮myButton=(按钮)findViewById(R.id.startButton);
最终按钮button1=(按钮)findViewById(R.id.button1);
最终按钮button2=(按钮)findViewById(R.id.button2);
最终按钮button3=(按钮)findViewById(R.id.button3);
最终按钮button4=(按钮)findViewById(R.id.button4);
myButton.setOnClickListener(
新建按钮。OnClickListener(){
公共void onClick(视图v){
((ViewGroup)myButton.getParent()).removeView(myButton);
颜色开关();
}
}
);
按钮4.setOnClickListener(
新建按钮。OnClickListener(){
公共void onClick(视图v){
myText.setText(“牛熊岩”);
//若按钮显示为黄色,则按钮为黄色

如果(牛熊[0]==2&&cowbears2[0]我找到了答案。你看,我的牛熊和牛熊整数的随机化方式不同


cowBears、cowBears 2、cowBears和cowBears 2最终整数必须直接在onCreate方法上方声明,因为在按下按钮的过程中会重新声明大写的cowBear整数。

我不确定它是否有效,但更改
private
(或
public
)的
最终
语句语句,然后再次运行应用程序。代码对我来说似乎没问题。