2021年4月29日星期四

Android keeps crashing

Am a new developer and really need help This is my android source code for school assesement. When I run the app it works fine. But when I press either of the buttons while some of the fields are empty, it will stop.[GUI of my app][1] I divided the interface into three sections; The first and the second include four TextViews and the last section has two TextViews When i input numbers in all the the Textviews in the first four or the second four, the app works fine but if i input numbers in some the TextViews in either sections and leave the rest, the app crashes

  import androidx.appcompat.app.AppCompatActivity;  import android.view.View;    import android.widget.EditText;  import android.os.Bundle;  import android.widget.TextView;  //import android.widget.Button;         public class MainActivity extends AppCompatActivity {       private TextView view1;       private TextView view2;       private TextView view3;       private TextView view4;  //     private EditText mk1;           EditText mk1, mk2, mk3, mk4, mk5, mk6, mk7, mk8, group, pject;       int sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8, sum9, sum10;       String s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;           double t1, t2;       int  t3, t4;    //TODO: Fix a bug         @Override       protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);  //        EditText mk1 = findViewById(R.id.mock1);           mk1 = findViewById(R.id.mock1);           mk2 = findViewById(R.id.m2);           mk3 = findViewById(R.id.m3);           mk4 = findViewById(R.id.m4);           mk5 = findViewById(R.id.m5);           mk6 = findViewById(R.id.m6);           mk7 = findViewById(R.id.m7);           mk8 = findViewById(R.id.m8);             group = findViewById(R.id.grou);           pject = findViewById(R.id.pro);             s1 = mk1.getText().toString();           s2 = mk2.getText().toString();           s3 = mk3.getText().toString();           s4 = mk4.getText().toString();           s5 = mk5.getText().toString();           s6 = mk6.getText().toString();           s7 = mk7.getText().toString();           s8 = mk8.getText().toString();           s9 = group.getText().toString();           s10 = pject.getText().toString();             view1 = findViewById(R.id.result);           view2 = findViewById(R.id.result2);           view3 = findViewById(R.id.result3);           view4 = findViewById(R.id.result4);         }         public boolean getNumbers() {           EditText mk1 = findViewById(R.id.mock1);           EditText mk2 = findViewById(R.id.m2);           EditText mk3 = findViewById(R.id.m3);           EditText mk4 = findViewById(R.id.m4);           EditText mk5 = findViewById(R.id.m5);           EditText mk6 = findViewById(R.id.m6);           EditText mk7 = findViewById(R.id.m7);           EditText mk8 = findViewById(R.id.m8);             EditText group = findViewById(R.id.grou);           EditText pject = findViewById(R.id.pro);             s1 = mk1.getText().toString();           s2 = mk2.getText().toString();           s3 = mk3.getText().toString();           s4 = mk4.getText().toString();           s5 = mk5.getText().toString();           s6 = mk6.getText().toString();           s7 = mk7.getText().toString();           s8 = mk8.getText().toString();           s9 = group.getText().toString();           s10 = pject.getText().toString();             view1 = findViewById(R.id.result);           view2 = findViewById(R.id.result2);           view3 = findViewById(R.id.result3);           view4 = findViewById(R.id.result4);               if ((s1.equals("") && s2.equals("") && s3.equals("") && s4.equals(""))) {               String result = "1st Result";               view1.setText(result);             } else {                 sum1 = Integer.parseInt(mk1.getText().toString());              sum2 = Integer.parseInt(mk2.getText().toString());              sum3 = Integer.parseInt(mk3.getText().toString());              sum4 = Integer.parseInt(mk4.getText().toString());           }           if ((s5.equals("") && s6.equals("") && s7.equals("") && s8.equals(""))) {               String result1 = "2nd Result";               view2.setText(result1);           } else {               sum5 = Integer.parseInt(mk5.getText().toString());               sum6 = Integer.parseInt(mk6.getText().toString());               sum7 = Integer.parseInt(mk7.getText().toString());               sum8 = Integer.parseInt(mk8.getText().toString());           }           if ((s9.equals(""))) {               String result2 = "Total";               view3.setText(result2);           } else {               sum9 = Integer.parseInt(group.getText().toString());           }           if ((s10.equals(""))) {               String result4 = "Final";               view4.setText(result4);           } else {               sum10 = Integer.parseInt(pject.getText().toString());           }             return true;       }         public void doCal1(View v) {          if (getNumbers()) {               t1 = (int) (((double) sum1 + sum2 + sum3 + sum4) / 400 * 20);             view1.setText(toString().valueOf(t1));       }             }       public void doCal2(View v) {           if (getNumbers()) {                 t2 = (int) (((double) sum5 + sum6 + sum7 + sum8) / 400 * 20);               view2.setText(toString().valueOf(t2));           }      }           public void doCal3 (View v){               if (getNumbers()) {                     t3 = (int) (t1 + t2 + sum9 + sum10);                   view3.setText(Float.toString(t3));               }           }           public void doCal4 (View v){               if (getNumbers()) {                   t4 = (int) (((float) t3) / 60 * 50);                   view4.setText(Integer.toString(t4));               }             }     }```        [1]: https://i.stack.imgur.com/Pvf2U.png  
https://stackoverflow.com/questions/67326930/android-keeps-crashing April 30, 2021 at 09:06AM

没有评论:

发表评论