【问题标题】:java Intellij errorjava Intellij 错误
【发布时间】:2012-09-24 04:45:57
【问题描述】:

这是我的 android 应用程序代码,它不完整

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText text;
    private EditText text2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.editText1);
        text2=(EditText)findViewById(R.id.result);


    }

    // This method is called at button click because we assigned the name to the
    // "OnClick property" of the button
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                float inputValue = Float.parseFloat(text.getText().toString());
                if (celsiusButton.isChecked()) {
                    text2.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(true);
                } else {
                    text2.setText(String
                            .valueOf(convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(false);
                    celsiusButton.setChecked(true);
                    if(KelvinButton.isChecked())
                    {
                        text2.setText(String.valueOf(convertoKelvin(inputValue)));
                    }
                }
                break;
            case R.id.button2:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
                float inputValue = Float.parseFloat(text.getText().toString());

                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                if (fahrenheitButton.isChecked()) {
                    text2.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(true);
                } else {
                    text2.setText(String
                            .valueOf(convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(true);
                    if(KelvinButton.isChecked())
                    {
                        text2.setText(String.valueOf(convertoKelvin(inputValue)));
                    }
                }
                break;
            case R.id.button3:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
                float inputValue = Float.parseFloat(text.getText().toString());
                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                if (KelvinButton.isChecked()) {
                    text2.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(true);
                } else {
                    text2.setText(String
                            .valueOf(convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(false);
                    celsiusButton.setChecked(true);
                    if(KelvinButton.isChecked())
                    {
                        text2.setText(String.valueOf(convertoKelvin(inputValue)));
                    }
                }
                break;

        }
    }

    // Converts to celsius
    private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
    }

    // Converts to fahrenheit
    private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
    }

    private float convertoKelvin(float celsius)
    {
        return ((celsius+273)) ;
    }


}

正如你所看到的,我每次都定义了我的变量。如果我不这样做,我会收到错误“值可能没有被初始化。我做错了什么。

如果您想知道,我的代码不完整。我很讨厌这个错误,一旦解决这个问题就会继续

【问题讨论】:

  • 你在哪一行得到这个错误?
  • at if (celsiusButton.isChecked())。这不是编译器警告,而是 IDE 警告

标签: java android intellij-idea


【解决方案1】:

如果我正确理解了您的问题如果您仅在一种情况下将值分配给celsiusButtonfahrenheitButtonKelvinButtonvariables,那么您会收到错误。

发生这种情况的原因是对象初始化的范围。以下是说明正在发生的事情的示例。

public void example() {
    String str;
    switch (1) {
    case 1:
        str = "test";// If I initalize here then there is problem since
                        // scope is limited to only this case
        str.toString();
        break;

    default:
        str.toString();// Compilation error here.
        break;
    }
}

public void example() {
    String str = "test";
    switch (1) {
    case 1:
        str.toString();
        break;

    default:
        str.toString();// No error because scope of initialisation is whole
                        // method
        break;
    }
}

所以你需要在switch (view.getId()) {之前定义你的变量

结果声明如下所示

public void onClick(View view) {
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
switch (view.getId()) {
    case R.id.button1:

【讨论】:

    【解决方案2】:

    如果我对您的理解正确,那么 IntelliJ 会警告您您正在使用的变量可能没有正确初始化。乍一看,如果在onClick() 之前没有调用onCreate(),那么texttext2 将不会被初始化。这很容易成为 IntelliJ 感到困惑的原因。

    【讨论】:

    • 如果局部变量未初始化,则会发生编译错误。我认为 IntelliJ 不会违反该规则。
    猜你喜欢
    • 2014-07-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 2020-04-23
    • 2014-06-22
    • 1970-01-01
    • 2019-03-18
    • 2017-04-02
    相关资源
    最近更新 更多