簡體   English   中英

如何在 Textview 中顯示計算值

[英]How to display the computed value in the Textview

我想知道為什么 public void onClick(View v) 方法中的代碼段不起作用。 當我單擊按鈕進行計算時,彈出框正在顯示並通知我:

應用程序意外停止

但是當我刪除那段代碼時沒有這樣的錯誤(在方法 public void onClick(View v) 中意味着問題出在該代碼上,而不是在清單文件上。

  import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.*;

   public class Form2 extends Activity 
   {

    String str1="";
    String str2="";
    String str3="";
    String str4="";
    String str5=""; 
    String str6="";
    String totalString = null;
    double num1=0;
    double num2=0;
    double num3=0;
    double num4=0;
    double num5=0;
    double num6=0;
    double a=0;
    double b=0;
    double c=0;
    double total=0;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form2);

      final  TextView nameTV=(TextView)findViewById(R.id.nameTV);
        nameTV.setText(getIntent().getExtras().getString("nameKey"));

        final TextView subjTV=(TextView)findViewById(R.id.subjTV);
        subjTV.setText(getIntent().getExtras().getString("subjectKey"));

        final TextView tv1=(TextView)findViewById(R.id.tv1);

        final EditText firstETxt=(EditText)findViewById(R.id.et1);

        final EditText firstAETxt=(EditText)findViewById(R.id.et1a);

        final EditText secondETxt=(EditText)findViewById(R.id.et2);

        final EditText secondAETxt=(EditText)findViewById(R.id.et2a);

        final EditText thirdETxt=(EditText)findViewById(R.id.et3);

        final EditText thirdAETxt=(EditText)findViewById(R.id.et3a);



        ImageButton calcuButt=(ImageButton)findViewById(R.id.calcB);

        calcuButt.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                if(v.getId()==R.id.calcB)
                {
                    str1=firstETxt.getText().toString();
                    num1=Double.parseDouble(str1);

                    str2=firstAETxt.getText().toString();
                    num2=Double.parseDouble(str2);

                    str3=secondETxt.getText().toString();
                    num3=Double.parseDouble(str3);

                    str4=secondAETxt.getText().toString();
                    num4=Double.parseDouble(str4);

                    str5=thirdETxt.getText().toString();
                    num5=Double.parseDouble(str5);

                    str6=thirdAETxt.getText().toString();
                    num6=Double.parseDouble(str6);

                    a=num1*num2;
                    b=num3*num4;
                    c=num5*num6;

                    total=a+b+c;

                    totalString = Double.toString(total);

                    tv1.setText(totalString);

            }}


        });

    }
}

這是xml代碼:

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout
    android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg"
    >
    <TextView
    android:id="@+id/widget33"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter all the scores you already have and"
    android:textSize="16sp"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_x="10px"
    android:layout_y="42px"
    >
    </TextView>
    <EditText
    android:id="@+id/et3"
    android:layout_width="57px"
    android:layout_height="35px"
    android:textSize="18sp"
    android:layout_x="10px"
    android:layout_y="232px"
    >
    </EditText>
    <EditText
    android:id="@+id/et1"
    android:layout_width="56px"
    android:layout_height="33px"
    android:textSize="13sp"
    android:layout_x="10px"
    android:layout_y="152px"
    >
    </EditText>
    <EditText
    android:id="@+id/et2"
    android:layout_width="55px"
    android:layout_height="34px"
    android:textSize="18sp"
    android:layout_x="10px"
    android:layout_y="192px" 
    >
    </EditText>
    <EditText
    android:id="@+id/et3a"
    android:layout_width="57px"
    android:layout_height="34px"
    android:textSize="18sp"
    android:layout_x="70px"
    android:layout_y="232px"
    >
    </EditText>
    <EditText
    android:id="@+id/et2a"
    android:layout_width="57px"
    android:layout_height="32px"
    android:textSize="18sp"
    android:layout_x="70px"
    android:layout_y="192px"
    >
    </EditText>
    <EditText
    android:id="@+id/et1a"
    android:layout_width="57px"
    android:layout_height="35px"
    android:textSize="13sp"
    android:layout_x="70px"
    android:layout_y="152px"
   >
   </EditText>

    <TextView
    android:id="@+id/tv1"
    android:layout_width="122px"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textColor="#ff000000"
    android:layout_x="120px"
    android:layout_y="162px"
    >
    </TextView>
    <TextView
    android:id="@+id/tv2"
    android:layout_width="128px"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textColor="#ff000000"
    android:layout_x="120px"
    android:layout_y="212px"
    >
    </TextView>
    <EditText
    android:id="@+id/ete"
    android:layout_width="98px"
    android:layout_height="37px"
    android:layout_x="120px"
    android:layout_y="242px"
    >
    </EditText>
    <TextView 
    android:layout_height="wrap_content"
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:text="SubjectTextView" 
    android:id="@+id/nameTV" 
    android:layout_width="wrap_content" 
    android:textStyle="bold" 
    android:layout_x="13dip" 
    android:layout_y="122dip">
    </TextView>
    <TextView android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="16sp" 
    android:text="the equivalent percentage of it in your subject" 
    android:typeface="sans" 
    android:id="@+id/widget34" 
    android:layout_width="wrap_content" 
    android:layout_x="13dip" 
    android:layout_y="74dip">
    </TextView>
    <TextView android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:text="nameTextView" 
    android:typeface="sans" 
    android:id="@+id/subjTV" 
    android:layout_width="144px" 
    android:textStyle="bold" 
    android:layout_x="48dip" 
    android:layout_y="32dip">
    </TextView>
    <TextView android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="16sp" 
    android:text="Hi" 
    android:typeface="sans" 
    android:id="@+id/widget30" 
    android:layout_width="wrap_content" 
    android:layout_x="22dip" 
    android:layout_y="34dip">
    </TextView>
    <ImageButton android:id="@+id/calcB" 
    android:layout_height="36px" 
    android:layout_width="100px" 
    android:src="@drawable/calc" 
    android:layout_x="122dip" 
    android:layout_y="329dip">
    </ImageButton>
    </AbsoluteLayout>

沒有看到完整的代碼,原因可能是以下之一:

  • 您的Double.parseDouble(strX)調用可能會拋出NumberFormatException - 如果字符串不包含可解析的雙Double.parseDouble(strX) 這意味着您需要確保它們都包含有效值。
  • 您的視圖之一為空,即尚未使用諸如secondETxt = (EditText) findViewById(R.id.YOURID);類的調用進行初始化secondETxt = (EditText) findViewById(R.id.YOURID);

我的猜測是您的EditText視圖之一是空的,導致上述異常。

我運行了你的代碼,它似乎有很多問題。 我不會給你諸如“避免絕對布局”之類的建議,或者你將來會學到的任何東西。

所以,第一次崩潰是在這行代碼

nameTV.setText(getIntent().getExtras().getString("nameKey"));

因此,如果您處於主要活動的上下文中,則不必從 Intent 類中獲取任何信息。 結果總是一樣的: crash 所以,首先定義你的值來自哪里。

其次,您必須在解析它們之前檢查 editTexts 中的值。它們不必為空,因為您將得到 NullPointerException 並且這些值必須至少為數字以避免 NumberFormatException。

最后,代碼行

 if(v.getId()==R.id.calcB)

沒有必要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM