>

Android 프로그래밍에 익숙하지 않으며 기본 앱을 프로그래밍하는 데 문제가 있습니다. 응용 프로그램은 코스에서 특정 점수를 얻기 위해 코스 시험에 필요한 점수를 계산해야합니다 (이것은 숙제 문제가 아니며 내가 작업중 인 것입니다). 특히 계산 결과를 textView 위젯에 표시하는 방법을 찾는 데 문제가 있습니다. 아래에 내 코드를 게시했습니다. 참고로, 내 앱은 시작할 때 즉시 충돌하므로 의심스러운 것을 발견하면 실제로 도움이 될 것입니다. 감사합니다!

업데이트 : hai hack 덕분에 이제 앱이 제대로 열립니다. 그러나 버튼을 클릭 한 후에도 여전히 충돌합니다. 변경 사항을 표시하도록 코드와 logcat을 업데이트했습니다

주요 활동 :

import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.widget.EditText;
 import android.widget.TextView;
 import android.widget.Button;
 import static com.managergmail.time.finite.finitemanager02.R.id.calculateButton;
 import static com.managergmail.time.finite.finitemanager02.R.id.textViewExamMarkNeeded;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   Button buttonCalculate = (Button) findViewById(R.id.buttonCalculate);
    buttonCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText currentGradeInput = (EditText) findViewById(R.id.currentGradeInput);
            float currentGradeValue = Float.valueOf(currentGradeInput.getText().toString());
            //obtaining value of currentGrade inputted by the user and converting to float
            EditText desiredGradeInput = (EditText) findViewById(R.id.desiredGradeInput);
            float desiredGradeValue = Float.valueOf(desiredGradeInput.getText().toString());
            //obtaining value of currentGrade inputted by the user and converting to float
            EditText examWeightInput = (EditText) findViewById(R.id.examWeightInput);
            float examWeightValue = Float.valueOf(examWeightInput.getText().toString());
            //obtaining value of currentGrade inputted by the user and converting to float
            float currentGradeWeight = 100-examWeightValue;
            //calculating current grade weight
            final float examMarkNeededValue;
            examMarkNeededValue= ((100*desiredGradeValue)-currentGradeValue*currentGradeWeight)/examWeightValue;
            textViewExamMarkNeeded.setText((Float.toString(examMarkNeededValue)));
        }
    });
}
}

xml 코드 :

<android.support.constraint.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
<TextView
    android:id="@+id/textView"
    android:layout_width="180dp"
    android:layout_height="45dp"
    android:text="@string/input_the_weighting_of_your_exam"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.078"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.356" />
<TextView
    android:id="@+id/textView2"
    android:layout_width="180dp"
    android:layout_height="45dp"
    android:text="@string/input_your_desired_grade"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.078"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.19" />
<TextView
    android:id="@+id/textViewExamMarkNeeded"
    android:layout_width="265dp"
    android:layout_height="64dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.662"
    app:layout_constraintHorizontal_bias="0.378" />
<TextView
    android:layout_width="180dp"
    android:layout_height="45dp"
    android:text="@string/input_your_current_grade"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.073"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.034"
    android:id="@+id/textView3" />
<EditText
    android:id="@+id/currentGradeInput"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:ems="10"
    android:inputType="numberDecimal"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="17dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView3"
    android:layout_marginEnd="33dp"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintTop_toTopOf="@+id/textView3"
    app:layout_constraintLeft_toRightOf="@+id/textView3" />
<EditText
    android:id="@+id/desiredGradeInput"
    android:layout_width="133dp"
    android:layout_height="31dp"
    android:ems="10"
    android:inputType="numberDecimal"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toTopOf="@+id/examWeightInput"
    android:layout_marginEnd="33dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="44dp"
    app:layout_constraintTop_toBottomOf="@+id/currentGradeInput"
    android:layout_marginBottom="42dp" />
<EditText
    android:id="@+id/examWeightInput"
    android:layout_width="0dp"
    android:layout_height="33dp"
    android:ems="10"
    android:inputType="numberDecimal"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toTopOf="@+id/calculateButton"
    android:layout_marginStart="2dp"
    android:layout_marginEnd="2dp"
    app:layout_constraintRight_toRightOf="@+id/desiredGradeInput"
    android:layout_marginTop="12dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="19dp"
    app:layout_constraintLeft_toLeftOf="@+id/desiredGradeInput"
    app:layout_constraintTop_toTopOf="@+id/textView" />
<Button
    android:id="@+id/buttonCalculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="17dp"
    android:layout_marginEnd="7dp"
    android:onClick="onButtonClick"
    android:text="@string/calculate"
    app:layout_constraintBottom_toTopOf="@+id/textViewExamMarkNeeded"
    app:layout_constraintRight_toRightOf="@+id/textViewExamMarkNeeded"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintRight_creator="1" />

로그 캣

12-17 21:14:05.840 18312-18312/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.managergmail.time.finite.finitemanager02, PID: 18312
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.managergmail.time.finite.finitemanager02/com.managergmail.time.finite.finitemanager02.MainActivity}: java.lang.NumberFormatException: Invalid float: ""
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647)
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
                                                   at android.os.Handler.dispatchMessage(Handler.java:111)
                                                   at android.os.Looper.loop(Looper.java:207)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5763)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
                                                Caused by: java.lang.NumberFormatException: Invalid float: ""
                                                   at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                   at java.lang.StringToReal.parseFloat(StringToReal.java:308)
                                                   at java.lang.Float.parseFloat(Float.java:306)
                                                   at java.lang.Float.valueOf(Float.java:343)
                                                   at com.managergmail.time.finite.finitemanager02.MainActivity.onCreate(MainActivity.java:20)
                                                   at android.app.Activity.performCreate(Activity.java:6280)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647) 
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                   at android.os.Looper.loop(Looper.java:207) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5763) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 

  • 답변 # 1

    이것을 editext에 추가해보십시오

       final EditText currentGradeInput = (EditText)findViewById(R.id.currentGradeInput);
    
       currentGradeInput.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
           }
           @Override
           public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                currentGradeValue = Float.valueOf(currentGradeInput.getText().toString());
           }
           @Override
           public void afterTextChanged(Editable editable) {
           }
       });
    
    

    위 코드는 숫자 형식 예외를 발생시키지 않고 완벽하게 작동합니다.

  • 답변 # 2

    examMarkNeededValue 인 것 같습니다   null 로 계산 . 따라서 다음을 사용하는 것이 좋습니다.

    String.valueOf(examMarkNeededValue)

    대신

    Float.toString(examMarkNeededValue)

    와이즈 비즈의 경우 인스턴스가 Float.toString 인 경우 그런 다음 null  던져졌지만이 경우에는 NullPointerException  문자열 String.valueOf 를 반환합니다 . 버튼에서 이벤트 리스너를 사용하지 않았기 때문에 계산이 null입니다.

  • 답변 # 3

    처음부터 EditText에는 값이 없었으므로 "null" 를 사용하십시오.  onCreate () 메서드에서 빈 문자열 ""을 부동 숫자로 변환하고 있음을 의미했습니다. 앱이 중단되는 것은 불가능합니다.

    이 경우 입력 값을 가져 와서 변환하려면 앱에 제출 단추가 있어야합니다. onCreate () 안에 다음을 추가하십시오.

    Float.valueOf(currentGradeInput.getText().toString())
    
    
    Button btnSubmit = findViewById(R.id.btn_button); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { float desiredGradeValue = Float.valueOf(desiredGradeInput.getText().toString()); float examWeightValue = Float.valueOf(examWeightInput.getText().toString()); textViewExamMarkNeeded.setText((Float.toString(examMarkNeededValue))); } });

관련 자료

  • 이전 이동 횟수가 너무 큼
  • 다음 node.js - Javascript for loop에 서버 측 지연을 추가하는 방법은 무엇입니까?