2012-09-24 7 views
1

이 당신이 내가 얻을 이렇게하지 내가 아주되었다 네 내 변수를 매번 정의에서 볼 수 있듯이자바 IntelliJ에 오류

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)) ; 
    } 


} 

완료되지 않은 내 안드로이드 응용 프로그램 내 코드입니다 오류가 "값은 내가 잘못하고 있어요 initialised.What되지 않았을 수 있습니다.

당신이 궁금해하는 경우, 내 코드가 아닌 완벽한 건이 오류에 빨아하고 계속됩니다 번이 고정되어

+0

아래에있는 라인은 당신이 오류가있는 것처럼 보일 것? –

+0

at if (celsiusButton.isChecked()). 컴파일러 경고가 아니며 IDE 경고입니다. – deception1

답변

2

질문을 올바르게 이해 한 경우 celsiusButton, fahrenheitButton, KelvinButton 변수에 값을 할당하면 오류가 발생합니다.

왜 이렇게되는지는 개체 초기화의 범위입니다. 다음은 현재 상황을 보여주는 샘플입니다.

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: 
0

내가 너를 올바르게 이해하고 있다면, IntelliJ는 사용하고있는 변수에 대해 경고하고있다. 제대로 초기화되었습니다. 한눈에 onCreate()onClick() 전에 호출되지 않으면 texttext2이 초기화되지 않습니다. 이것은 IntelliJ가 혼란스러운 이유 일 수 있습니다.

+0

로컬 변수가 초기화되지 않은 경우 컴파일 오류가 발생합니다. 나는 IntelliJ가 그 규칙을 위반할 것이라고 생각하지 않는다. –