2011-12-27 107 views
0

由於某些原因,它告訴我TextView變量fact1和fact2尚未初始化。我通過代碼在2/3內使用它們,在關於sol1和sol2的情況下。請幫忙!謝謝!局部變量尚未初始化?

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

回答

4

在初始化它們之前,您可以調用setText()。移動設置它們的代碼。

+0

謝謝你,成功了! – Wilson 2011-12-27 01:26:35

+0

那麼你能接受答案嗎?它將幫助您解答未來的問題。 – 2011-12-27 01:27:08

+0

是的!我必須等3分鐘,但我會。 – Wilson 2011-12-27 01:31:24

0

初始化fact1fact2當你聲明它們,就像這樣:

TextView fact1 = (TextView) findViewById(R.id.tvfact1); 
TextView fact2 = (TextView) findViewById(R.id.tvfact2);