按下按鈕後應用程序崩潰。按下按鈕後,結果沒有顯示出來。用於計算提示的Android應用程序
下面是堆棧跟蹤:
FATAL EXCEPTION: FATAL EXCEPTION: main
Process: com.example.btyagi.tipcalculator, PID: 8357
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:312)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4417)
at com.example.btyagi.tipcalculator.MainActivity$1.onClick(MainActivity.java:54)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
這是我的Activity類:
public class MainActivity extends AppCompatActivity {
EditText totalBill;
EditText totalTip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText checkAmount = (EditText) findViewById(R.id.editText);
final EditText noOfPeople = (EditText) findViewById(R.id.editText2);
final EditText tipPercent = (EditText) findViewById(R.id.editText3);
totalBill = (EditText) findViewById(R.id.editText4);
totalTip = (EditText) findViewById(R.id.editText5);
//TextView totalPerPerson = (TextView) findViewById(R.id.textView5);
//TextView tipPerPerson = (TextView) findViewById(R.id.textView4);
Button calculateButton = (Button) findViewById(R.id.button);
// ArrayList<String> list = new ArrayList<String>();
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list);
// try {
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int amount = Integer.parseInt(checkAmount.getText().toString());
int people = Integer.parseInt(noOfPeople.getText().toString());
int tip = Integer.parseInt(tipPercent.getText().toString());
totalBill.setText(new StringBuilder().append(amount).append(tip/100 * amount).toString());
totalTip.setText(tip/100 * amount);
// totalPerPerson.setText(totalBill.get/people);
} catch (NumberFormatException ed) {
System.out.print("Not a number");
}
}
});
//
}
}
XML文件內容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.btyagi.tipcalculator.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Check Amount"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="78dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No. of People"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tip %"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText"
android:layout_above="@+id/textView2"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText2"
android:layout_above="@+id/textView3"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText3"
android:layout_above="@+id/button"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText4"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText5"
android:layout_below="@+id/editText4"
android:layout_alignLeft="@+id/editText4"
android:layout_alignStart="@+id/editText4" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText6"
android:layout_below="@+id/editText5"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText7"
android:layout_below="@+id/editText6"
android:layout_alignRight="@+id/editText6"
android:layout_alignEnd="@+id/editText6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TipPerPerson"
android:id="@+id/textView4"
android:layout_alignBottom="@+id/editText7"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TotalPerPerson"
android:id="@+id/textView5"
android:layout_alignBottom="@+id/editText6"
android:layout_alignLeft="@+id/textView4"
android:layout_alignStart
爲什麼你永遠不會從你的佈局引用你的EditText視圖?這不是用戶輸入值的地方嗎? –
[TextView中的整數值]的可能重複(http://stackoverflow.com/questions/3994315/integer-value-in-textview) –