java - 局部变量还没有初始化?

标签 java android variables initialization textview

由于某种原因,它告诉我 TextView 变量fact1和fact2尚未初始化。我在代码中大约 2/3 使用了它们,在有关 sol1 和 sol2 的 if else 中。请帮忙!谢谢!

package boston.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TheBostonProjectActivity extends Activity {

    public EditText aed, bed, ced;
    public TextView dtv, nstv, sol1tv, sol2tv, factortv;
    public int a, b, c, dis;
    public Button solve;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        solve = (Button) (findViewById(R.id.bsolve));
        solve.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Perform action on click

                aed = (EditText) (findViewById(R.id.etA));
                try {
                    a = Integer.parseInt(aed.getText().toString());
                } catch (NumberFormatException e) {
                    a = 0;
                }
                bed = (EditText) (findViewById(R.id.etB));
                try {
                    b = Integer.parseInt(bed.getText().toString());
                } catch (NumberFormatException e) {
                    b = 0;
                }
                ced = (EditText) (findViewById(R.id.etC));
                try {
                    c = Integer.parseInt(ced.getText().toString());
                } catch (NumberFormatException e) {
                    c = 0;
                }

                dtv = (TextView) findViewById(R.id.tvdis);
                dis = (b * b) - (4 * a * c);
                dtv.setText("Discriminant: " + dis);
                nstv = (TextView) findViewById(R.id.tvnumsol);
                sol1tv = (TextView) findViewById(R.id.tvsol1);
                sol2tv = (TextView) findViewById(R.id.tvsol2);
                if (dis > 0) {
                    double sol1, sol2;
                    TextView fact1, fact2;
                    sol1 = ((-b) + ((b * b) + (4 * a * c)) ^ (1 / 2)) / (2 * a);
                    sol2 = ((-b) + ((b * b) - (4 * a * c)) ^ (1 / 2)) / (2 * a);
                    sol1tv.setText("Solution 1: " + sol1);
                    sol2tv.setText("Solution 2: " + sol2);
                    nstv.setText("The equation will have 2 solutions.");
                    if (sol1 > 0) {
                        fact1.setText("(x-" + sol1 + ")");
                    } else {
                        double sol1pos;
                        sol1pos = Math.abs(sol1);
                        fact1.setText("(x+" + sol1pos + ")");
                    }
                    if (sol2 > 0) {
                        fact2.setText("(x+" + sol2 + ")");
                    } else {
                        double sol2pos;
                        sol2pos = Math.abs(sol2);
                        fact1.setText("(x+" + sol2pos + ")");
                    }
                    fact1 = (TextView) findViewById(R.id.tvfact1);
                    fact2 = (TextView) findViewById(R.id.tvfact2);
                } else if (dis == 0) {
                    double sol1;
                    String fact;

                    sol1 = (-b) / (2 * a);
                    sol1tv.setText("Solution 1: " + sol1);
                    sol2tv.setText("No second solution");
                    nstv.setText("The equation will have 1 solution.");

                    if (sol1 > 0) {
                        fact = ("(x+" + sol1 + ")²");
                    } else {
                        fact = ("(x-" + sol1 + ")²");
                    }
                } else {
                    sol1tv.setText("No second solution");
                    sol2tv.setText("No second solution");
                    nstv.setText("The equation will have no solutions.");
                }
            }
        });
    }
}

最佳答案

在初始化它们之前,您可以对它们调用setText()。移动设置它们的代码。

关于java - 局部变量还没有初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640358/

相关文章:

java - Java 中循环的局部变量初始化最有效的方法是什么?

oracle - 在 Oracle 中定义局部变量的最简单方法是什么?

java - 列出所有 liquibase sql 类型

java - 如何根据特定条件从文本文件中删除一行

java - 在 Java 中寻找 UPDATE SQL 的模式

java - 如何在Java中批量执行一组 'select count(*)'查询?

android - 如何在fragment中实现onBackPressed并到达HomeFragment?

Android 支持 Repo 46.0.0 和 Android Studio 2.3

javascript - android 4.0 中屏蔽输入文本框的问题

java - 变量初始化