按下按钮后应用程序崩溃。
下面的是堆栈跟踪:
tempnam
这是我的活动课程:
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)
XML文件内容:
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");
}
}
});
//
}
}
答案 0 :(得分:2)
最终变量只能初始化一次。
更改 totalBill , totalTip 和 totalPerPerson TextView
(实际上xml中有EditText
个)转换为类变量,并将amount
,people
和tip
移动到点击监听器。
public class MainActivity extends AppCompatActivity {
TextView totalBill;
TextView totalTip;
TextView totalPerPerson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView checkAmount = (TextView) findViewById(R.id.textView);
TextView noOfPeople = (TextView) findViewById(R.id.textView2);
TextView tipPercent = (TextView) findViewById(R.id.textView3);
totalBill = (TextView) findViewById(R.id.textView7);
totalTip = (TextView) findViewById(R.id.textView6);
totalPerPerson = (TextView) findViewById(R.id.textView5);
TextView tipPerPerson = (TextView) findViewById(R.id.textView4);
Button calculateButton = (Button) findViewById(R.id.button);
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(String.valueOf(tip / 100 * amount));
// totalPerPerson.setText(totalBill.get/people);
} catch (NumberFormatException ex) {
System.out.print("Not a number");
}
}
});
}
}
答案 1 :(得分:0)
这一行
totalTip.setText(tip / 100 * amount);
应该这样说
totalTip.setText(String.valueOf(tip / 100 * amount));
原因是setText(int resId)
是错误的方法,并且会抛出你正在获得的Resources$NotFoundException
。