2017-12-18 47 views
0

我是Android編程的新手,我遇到了編程基本應用程序的問題。該應用程序應該計算課程考試所需的標記,以獲得課程中的某個標記(這不是一個家庭作業問題,而只是我正在研究的內容)。具體來說,我無法找到一種方法來在textView小部件中顯示計算結果。我已經在下面發佈了我的代碼。另一方面,我的應用程序在啓動時也會立即崩潰,所以如果你發現任何可疑的東西,那真的會有幫助。謝謝!我如何讓Android Studio在textView中顯示計算的值

更新:感謝hai hack,我的應用程序現在可以正常打開。但是,點擊按鈕後它仍然崩潰。我有更新我的代碼和logcat的顯示所做的更改

MainActivity:

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" /> 

logcat的

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)  
+0

你的應用程序發生了哪些崩潰?你可以包括錯誤logcat? –

+0

我添加了logcat。看起來浮動值存在問題,但我不知道如何解決它 –

+0

所以你想要動態地進行計算或只需要點擊提交按鈕來完成計算? –

回答

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) { 

     } 
    }); 

上面的代碼將很好地工作而不引發任何數字格式例外。

+0

是這個代碼應該放在OnCreate()?另外,我還需要什麼進口聲明?再次感謝您的幫助 –

+0

不只是聲明您的editext並添加上面的代碼 –

+0

另外,在我的java文件中,setText方法以紅色高亮顯示,它表示「無法解析方法'setText(java.lang.String)' –

0

看來, examMarkNeededValue計算爲null。所以我建議你使用:

String.valueOf(examMarkNeededValue)

,而不是

Float.toString(examMarkNeededValue)

因爲在Float.toString情況下,如果實例null,然後NullPointerException被拋出,但在這種情況下String.valueOf會返回一個字符串"null"。 計算結果爲空,因爲您沒有在按鈕上使用事件偵聽器。

+0

我試圖改成'String.valueOf(examMarkNeededValue)',但我仍然有類似的錯誤。 –

0

從一開始,你的EditTexts就沒有價值,所以在onCreate()方法中使用Float.valueOf(currentGradeInput.getText().toString())意味着你將一個空字符串「」轉換爲浮點數。這不可能讓你的應用崩潰。

在這種情況下,您應該在應用中有一個提交按鈕來獲取輸入值並將其轉換。裏面的onCreate(),添加:

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

嗨,很抱歉,我對android編程很陌生。我在哪裏將這段代碼放在我的java文件中?此外,我是否必須導入任何新的東西或必須在xml中創建一個新按鈕才能使用?再次感謝! –

+0

@SeanW是的,您必須在Xml中創建新的提交按鈕,並將所有在輸入值'Float.valueOf'內的行移動到onClick()'方法內部。看到我編輯的答案。 –

相關問題