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

时间:2019-04-14 11:02:26

标签: java android

我正在尝试构建一个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();
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

我看到2种解决方案:

1) 执行所需操作的最简单方法是,不使用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);