繁体   English   中英

为什么不能从另一个类(另一个Java文件)访问按钮值?

[英]Why does the button values cannot be accessed from another class (other java file)?

我正在尝试构建一个Android应用,使用户可以在一定时间内进行一些计算。 该代码运行良好,直到我将代码分为两部分并创建了另一个类来执行其他任务。

我已经将所有相应的包和类文件导入到新类中。代码中没有错误,但是当我运行应用程序时,按钮和textview没有显示任何内容。我尝试了多次更改代码,但没有用。 当我将所有代码合并到一个类中时,代码效果很好。

该错误显示为:

尝试在Logic.java中调用虚拟方法“ b1.setText(Integer.toString(answers [0]))”。

每个按钮和textview显示相同的错误。 此错误是Nullpointer Exception 我该如何运作? 任何帮助表示赞赏。

MainActivity.java

package e.nani.firstattempt;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public int a1;//random num 1
    public int a2;//random num 2;
    public TextView textview;
    public Button b1;
    public Button b2;
    public Button b3;
    public Button b4;
    public int option1;
    public int option2;
    public int option3;
    public int option4;
    public int score = 0;
    TextView scoreid;
    int numberofquestions = 10;
    TextView time;
    public int answers[] = new int[4];
    Logic c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView) findViewById(R.id.sum);
        b1 = (Button) findViewById(R.id.option1);
        b2 = (Button) findViewById(R.id.option2);
        b3 = (Button) findViewById(R.id.option3);
        b4 = (Button) findViewById(R.id.option4);
        time = (TextView) findViewById(R.id.timer);

        scoreid = (TextView) findViewById(R.id.scoreid);
        scoreid.setText((0 + "/" + numberofquestions));


        c.operatio();

        timer.start();

    }


    public void operation(View V) {
        try {
            switch (V.getId()) {
                case R.id.option1:

                    if (b1.getText().equals(Integer.toString(option4))) {
                        score = score + 1;
                        c.operatio();
                        scoreid.setText((score + "/" + numberofquestions));
                    } else {

                        Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
                        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(500);
                        c.operatio();
                    }
                    break;
                case R.id.option2:


                    if (b2.getText().equals(Integer.toString(option4))) {
                        score = score + 1;
                        c.operatio();
                        scoreid.setText(score + "/" + numberofquestions);
                    } else {

                        Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
                        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(500);
                        c.operatio();
                    }
                    break;
                case R.id.option3:
                    if (b3.getText().equals(Integer.toString(option4))) {
                        score = score + 1;
                        c.operatio();
                        scoreid.setText((score + "/" + numberofquestions));
                    } else {
                        Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
                        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(500);
                        c.operatio();
                    }
                    break;

                case R.id.option4:
                    if (b4.getText().equals(Integer.toString(option4))) {
                        score = score + 1;
                        c.operatio();
                        scoreid.setText(score + "/" + numberofquestions);
                    } else {
                        Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
                        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(500);
                        c.operatio();
                    }

                    break;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    CountDownTimer timer = new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            time.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            time.setText("done!");
        }
    };


}

Logic.java

package e.nani.firstattempt;

import java.util.Random;

class Logic extends MainActivity {

    public void operatio() {
        try {
            Random n = new Random();
            int n1 = n.nextInt(4);
            int n2 = n.nextInt(4);
            int n3 = n.nextInt(4);
            int n4 = n.nextInt(4);

            a1 = n.nextInt(51);
            a2 = n.nextInt(35);
            option1 = n.nextInt((a1 + a2) + 1);

            option2 = n.nextInt((a1 + a2) + 1);
            option3 = n.nextInt((a1 + a2) + 1);
            option4 = a1 + a2;


            answers[n1] = option1;
            while (n2 == n1) {
                n2 = n.nextInt(4);
            }
            while (option2 == option1 || option2 == option4) {

                option2 = nextInt((a1 + a2) + 1);

            }


            answers[n2] = option2;
            while (option3 == option2 || option3 == option1 || option3 == option4) {
                option3 = n.nextInt((a1 + a2) + 1);
            }


            while (n3 == n2 || n3 == n1) {
                n3 = n.nextInt(4);
            }

            answers[n3] = option3;

            while (n4 == n2 || n4 == n1 || n4 == n3) {
                n4 = n.nextInt(4);
            }
            answers[n4] = option4;


            b1.setText(Integer.toString(answers[0]));
            b2.setText(Integer.toString(answers[1]));
            b3.setText(Integer.toString(answers[2]));
            b4.setText(Integer.toString(answers[3]));
            textview.setText(a1 + "+" + a2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这里的主要问题是,为什么当代码仅在主类中时应用程序正常工作,而在其他类中编写某些代码时却无法工作? 谢谢。

这是因为您应该传递上下文并做一些技巧。

我看到2个解决方案:

1)做您想要的事情的最简单的方法是使用public void operatio()而不是使用public void operatio() public String operatio()并返回要设置的字符串。

然后在主,这样称呼它:

textView.setText( c.operatio()); 这将设置您返回的字符串。

2)使用上下文。

您将MainActivity的上下文传递给函数。

逻辑类:

该函数应为:

public void operatio(Context context)

在您的逻辑类中,使用:

TextView txtView = (TextView) ((Activity)context).findViewById(R.id.sum);
textview.setText(a1 + "+" + a2);

主要活动

然后,调用它: c.operatio(MainActivity.this);

暂无
暂无

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

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