2016-06-22 9 views
2

In meinem Code benutze ich simple editText und textView, benutze editText get value und berechne in main.java. Und schreiben Sie das Ergebnis bei textview.EditText und TextView usw.

Aber das Problem in App läuft, wird es

Verursacht durch: java.lang.NullPointerException

...

...

Bei com.example .jk.kktae0034_sec.MainActivity. (MainActivity.java:52)

In der Leitung 52 ist, dass

TextView recText = (TextView) findViewById (R.id.rec_WH); //here is first error 

... 

EditText editFck = (EditText) findViewById (R.id.fck); //if first error is // than here is occur error 

public class MainActivity extends AppCompatActivity { 

    Button btnCal; 

    TextView recText = (TextView)findViewById(R.id.rec_WH); 
    TextView wheelStr = (TextView)findViewById(R.id.wheelStr); 
    TextView ironStr = (TextView)findViewById(R.id.ironStr); 

    EditText editFck = (EditText)findViewById(R.id.fck); 
    EditText editFy = (EditText)findViewById(R.id.fy); 
    EditText edit_life_load = (EditText)findViewById(R.id.life_load); 
    EditText edit_dead_load = (EditText)findViewById(R.id.dead_load); 
    EditText editL = (EditText)findViewById(R.id.l); 
    EditText editRatio = (EditText)findViewById(R.id.ratio); 

    double b, d, roh, Mn, Mu, pi=0.85, w, L, fy, fck, roh_max, roh_min, h, a, c, beta1, strain_t, As; 
    double life_load, dead_load, ratio; 

    double AA[][] = { 
     {9.53,71.3,142.6,213.9,285.2,356.5,427.8,499.1,570.4,641.7,713}, 
     {12.7,126.7,253.4,380,1,506.8,633.5,760.2,886.9,1014,1140}, 
     {15.9,198.6,397.2,595.8,794.4,993.0,1191,1390,1589,1787,1986}, 
     {19.1,286.5,573.0,859.5,1146,1433,1719,2006,2292,2579,2865}, 
     {22.2,387.1,774.2,1161,1548,1935,2322,2710,3097,3484,3871}, 
     {25.4,506.7,1013.4,1520,2027,2534,3040,3547,4054,4560,5067}, 
     {28.6,642.4,1285,1927,2570,3212,3854,4497,5139,5782,6424}, 
     {31.8,794.2,1588,2383,3177,3971,4765,5559,6354,7148,7942}, 
     {34.9,956.6,1913,2870,3826,4783,5740,6696,7653,8609,9566}, 
     {38.1,1140,2280,3420,4560,5700,6840,7980,9120,10260,11400} 
    }; 

    int num[] = {10,13,16,19,22,25,29,32,35,38 }; 

    public void settingHeight(int i){ 
     int j, cnt=0; 
     h = d + 0.5 * AA[i][0] + 12.7 + 40; 
     for(j = 0; j < 10000; j++){ 
      if(j*10 > h){ 
       h = j*10; 
       cnt++; 
       break; 
      } 
      if(cnt > 0) 
       break; 
     } 
     d = h - (0.5 * AA[i][0] + 12.7 + 40); 
    } 

    void reSetting(){ 
     a = As * fy/(0.85 * fck * b); 
     c = a/beta1; 
     strain_t = (d - c)/c * 0.003; 
     if (strain_t > 0.005) 
      pi = 0.85; 
     else if (strain_t > 0.002) 
      pi = 0.65 + (0.0043 - 0.002) * 200/3; 
     else 
      pi = 0.65; 
     Mn = As * fy * (d - a/2); 
     Mu = pi * Mn; 

     recText.setText("B = "+String.format("%.2f", b)+"mm, D = "+String.format("%.2f", d)+"mm"); 
     wheelStr.setText("Mu = "+String.format("%.2f", Mu/1000000)+"KN*m"); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     fck = Double.parseDouble(editFck.getText().toString()); 
     fy = Double.parseDouble(editFy.getText().toString()); 
     life_load = Double.parseDouble(edit_life_load.getText().toString()); 
     dead_load = Double.parseDouble(edit_dead_load.getText().toString()); 
     L = Double.parseDouble(editL.getText().toString()); 
     ratio = Double.parseDouble(editRatio.getText().toString()); 

     btnCal = (Button)findViewById(R.id.btnCal); 

     btnCal.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //steel ratio 
       roh = 0.85 * 0.85 * fck/fy * 0.003/(0.003 + 0.005); 

       //main 
       w = 1.2 * dead_load + 1.6 * life_load; 
       Mu = w * L*L/8; 

       //setting side 
       int i, cnt = 0; 
       double bdd; 
       bdd = Mu*1000000/(pi*roh*fy*(1 - 0.59*roh*fy/fck)); 
       bdd = bdd/(ratio*ratio); 
       b = Math.pow(bdd,0.33333333); 

       for (i = 1;i<=10000;i++) { 
        if (i * 10 > b) { 
         b = i * 10; 
         cnt++; 
         break; 
        } if (cnt > 0) 
         break; 
       } 
       d = b * ratio; 

       //setting steel 
       int x, y, cnt_1, cnt_2; 
       double Ass; 
       Ass = roh * b * d; 
       for (x = 0; x < 10; x++) { 
        for (y = 0; y < 10; y++) { 
         if (AA[x][y] >= Ass) { 
          roh_max = 0.85 * 0.85 * fck/fy * 0.003/0.007; 
          if (0.25 * Math.sqrt(fck)/fy > 1.4/fy) 
           roh_min = 1.4/fy; 
          else 
           roh_min = 0.25 * Math.sqrt(fck)/fy; 
          roh = AA[x][y]/b/d; 
          if (roh > roh_min && roh < roh_max) { 
           cnt_2 = y; 
           cnt_1 = x; 
           As = AA[x][y]; 

           ironStr.setText("철근"+cnt_2+" -D "+num[cnt_1]); 
           settingHeight(cnt); 
           reSetting(); 
          } 
         } 
        } 
       } 
      } 
     }); 
    } 
} 

Layout-

<RelativeLayout 
    android:id="@+id/mainRelayout"> 

    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/input_text_Layout"> 

     <TextView 
      android:id="@+id/life_load_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Life_Load" 
      android:textSize="15pt"/> 

     <TextView 
      android:id="@+id/dead_load_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Dead_Load" 
      android:textSize="15pt" 
      android:layout_below="@+id/life_load_text"/> 

     <TextView 
      android:id="@+id/l_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="L" 
      android:textSize="15pt" 
      android:layout_below="@+id/dead_load_text"/> 

     <TextView 
      android:id="@+id/fck_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Fck" 
      android:textSize="15pt" 
      android:layout_below="@+id/l_text"/> 

     <TextView 
      android:id="@+id/fy_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Fy" 
      android:textSize="15pt" 
      android:layout_below="@+id/fck_text"/> 

     <TextView 
      android:id="@+id/ratio_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="ratio" 
      android:textSize="15pt" 
      android:layout_below="@+id/fy_text"/> 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/input_Layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_toRightOf="@+id/input_text_Layout"> 

     <EditText 
      android:id="@+id/life_load" 
      android:labelFor="@+id/life_load" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:ems="10" /> 

     <EditText 
      android:id="@+id/dead_load" 
      android:labelFor="@+id/dead_load" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:ems="10" 
      android:layout_below="@+id/life_load"/> 

     <EditText 
      android:id="@+id/l" 
      android:labelFor="@+id/l" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:ems="10" 
      android:layout_below="@+id/dead_load"/> 

     <EditText 
      android:id="@+id/fck" 
      android:labelFor="@+id/fck" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:ems="10" 
      android:layout_below="@+id/l"/> 

     <EditText 
      android:id="@+id/fy" 
      android:labelFor="@+id/fy" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:ems="10" 
      android:layout_below="@+id/fck"/> 

     <EditText 
      android:id="@+id/ratio" 
      android:labelFor="@+id/ratio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:ems="10" 
      android:layout_below="@+id/fy"/> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/input_Layout" 
     android:id="@+id/screen_value"> 

     <TextView 
      android:id="@+id/rec_WH" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15pt" 
      android:text="width_height" /> 

     <TextView 
      android:id="@+id/wheelStr" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15pt" 
      android:text="wheelStrongth" 
      android:layout_below="@+id/rec_WH"/> 

     <TextView 
      android:id="@+id/ironStr" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15pt" 
      android:text="ironStrength" 
      android:layout_below="@id/wheelStr"/> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/input_Layout" 
     android:layout_toRightOf="@+id/screen_value"> 

     <Button 
      android:id="@+id/btnCal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="calculation" /> 

     <Button 
      android:id="@+id/btnNext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="next" 
      android:layout_below="@+id/btnCal"/> 

    </RelativeLayout> 

</RelativeLayout> 
+0

Beitrag in onCreate Logcat –

+0

zu setzen Spuren Ich bin nicht sicher, es ist die Lösung für Sie aber ruft die findViewById in onCreate auf, um sicher zu sein, anstatt auf Klassenebene. Weil es NullpointerExceptions verursachen kann. Der Grund ist, dass Sie versuchen, auf eine ID zuzugreifen, die noch nicht erstellt wurde. –

+0

THX für Ihre Hilfe, ich werde in meinem Haus nach der Arbeit tun :) – JKLee

Antwort

3

Alle in onCreate instanziiert werden soll() ..

TextView recText ,wheelStr,ironStr ; 
EditText editFck ,editFy ,edit_life_load,edit_dead_load,editL,editRatio; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     recText = (TextView)findViewById(R.id.rec_WH); 
    wheelStr = (TextView)findViewById(R.id.wheelStr); 
    ironStr = (TextView)findViewById(R.id.ironStr); 

    editFck = (EditText)findViewById(R.id.fck); 
    editFy = (EditText)findViewById(R.id.fy); 
    edit_life_load = (EditText)findViewById(R.id.life_load); 
    edit_dead_load = (EditText)findViewById(R.id.dead_load); 
    editL = (EditText)findViewById(R.id.l); 
    editRatio = (EditText)findViewById(R.id.ratio); 
    ..................... 
    ................... 
+2

Mehr wie "nach' setContentView (R.layout.activity_main) '" als "in' onCreate() '" aber immer noch, das ist wahrscheinlich das Problem. –

+0

Dies ist das zweite Problem, zuerst gelöst ... zeigen Sie Ihr nächstes Problem, dude .. können überprüfen .. – Kush

+0

Was? Es tut mir leid, ich glaube nicht, dass ich verstehe, was Sie sagen ... –

0

benötigen Sie id von Ressourcen in onCreate() -Methode assing:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editFck = (EditText)findViewById(R.id.fck); 
    ...... 
} 
+0

Das ist falsch, die findViewById sollte nur nach setContentView passieren – Elye

+0

@Elye ja ich habe meine Antwort bearbeitet. Vielen Dank! –

1

Sie können nicht initialisiert werden Elemente oder Kontrollen außerhalb von onCreate() Funktion

Sie werden in der es haben zu initialisieren .

public class MainActivity extends AppCompatActivity { 

    Button btnCal; 
    TextView recText, wheelStr, ironStr; 
    EditText editFck, editFy, edit_life_load, edit_dead_load, editL, editRatio; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     /* OTHER CODE */ 
     recText = (TextView)findViewById(R.id.rec_WH); 
     wheelStr = (TextView)findViewById(R.id.wheelStr); 
     ironStr = (TextView)findViewById(R.id.ironStr); 
     editFck = (EditText)findViewById(R.id.fck); 
     editFy = (EditText)findViewById(R.id.fy); 
     edit_life_load = (EditText)findViewById(R.id.life_load); 
     edit_dead_load = (EditText)findViewById(R.id.dead_load); 
     editL = (EditText)findViewById(R.id.l); 
     editRatio = (EditText)findViewById(R.id.ratio); 
     /* OTHER CODE */ 
    } 
} 
0

Statt wie zu tun

public class MainActivity extends AppCompatActivity { 
Button btnCal; 

TextView recText = (TextView)findViewById(R.id.rec_WH); 
TextView wheelStr = (TextView)findViewById(R.id.wheelStr); 
TextView ironStr = (TextView)findViewById(R.id.ironStr); 

EditText editFck = (EditText)findViewById(R.id.fck); 
EditText editFy = (EditText)findViewById(R.id.fy); 
EditText edit_life_load = (EditText)findViewById(R.id.life_load); 
EditText edit_dead_load = (EditText)findViewById(R.id.dead_load); 
EditText editL = (EditText)findViewById(R.id.l); 
EditText editRatio = (EditText)findViewById(R.id.ratio); 

Sie haben alle diese

TextView recText ,wheelStr,ironStr ; 
EditText editFck ,editFy ,edit_life_load,edit_dead_load,editL,editRatio; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    recText = (TextView)findViewById(R.id.rec_WH); 
    wheelStr = (TextView)findViewById(R.id.wheelStr); 
    ironStr = (TextView)findViewById(R.id.ironStr); 

    editFck = (EditText)findViewById(R.id.fck); 
    editFy = (EditText)findViewById(R.id.fy); 
    edit_life_load = (EditText)findViewById(R.id.life_load); 
Verwandte Themen