尝试了多种解决此问题的方法,但到目前为止没有任何效果,错误仅发生在click上,因此请确保其在我的
orderSummaryTextView_andPrice
,但我似乎找不到问题。你们中有人可以发现我做错了吗?
Java主类:
公共类MainActivity扩展了AppCompatActivity {
Button minusOneButton, plusOneButton, orderButton;
TextView quanityTextView, orderSummaryTextView_andPrice;
CheckBox whipped_cream_check_box, chocolate_checkBox;
EditText user_inputted_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
minusOneButton = findViewById(R.id.minusOneButton);
plusOneButton = findViewById(R.id.plusOneButton);
orderButton = findViewById(R.id.orderButton);
whipped_cream_check_box = findViewById(R.id.whipped_cream_check_box);
chocolate_checkBox = findViewById(R.id.chocolate_checkBox);
user_inputted_name = findViewById(R.id.user_inputted_name);
quanityTextView = (TextView) findViewById(R.id.quanityTextView);
orderSummaryTextView_andPrice = findViewById(R.id.orderSummaryTextView_andPrice);
}
// coffee will cost 1 dollar with an added dollar for each topping
int quanity = 0;
public void submitOrder(View view) {
calculatePrice();
createOrderSummery();
}
public void createOrderSummery(){ // titl
EditText userName = (EditText) findViewById(R.id.user_inputted_name);
orderSummaryTextView_andPrice.setText(String.valueOf("Name: " + userName.getText().toString() +
"\nAdd whipped creame? "+ whipped_cream_check_box.isChecked()+
"\nAdd chocolate? " + chocolate_checkBox.isChecked() +
"\nQuanity: " + quanity +
"\nTotal: $" + orderSummaryTextView_andPrice.getText() +
"\nThank you!")); // creats the summery text onclick
Log.d("MainActivity.class","Create order summary method finished");
}
public void calculatePrice(){
int price = Integer.parseInt(orderSummaryTextView_andPrice.getText().toString()); // price = the value of textview 2 in int form
displayMessage(price); // external method
}
public void displayMessage(int number){
TextView orderSummaryTextView = (TextView) findViewById(R.id.orderSummaryTextView_andPrice); // price = order_summary_text_view
orderSummaryTextView.setText(String.valueOf(number)); // converted back to a string then displays
}
public void increment(View view) {
quanity += 1; // this is just a global var
quanityTextView.setText(String.valueOf(quanity));//we're converting quanity into a string with the empty quotes so it can be displayed
}
public void decrement(View view) {
quanity -= 1;
quanityTextView.setText(String.valueOf(quanity));
}
}
Logcat错误:
2019-01-22 12:37:30.126 23015-23015/com.example.coffee E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.coffee, PID: 23015
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6308)
at android.widget.TextView.performClick(TextView.java:11202)
at android.view.View$PerformClick.run(View.java:23969)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6308)
at android.widget.TextView.performClick(TextView.java:11202)
at android.view.View$PerformClick.run(View.java:23969)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at com.example.coffee.MainActivity.calculatePrice(MainActivity.java:61)
at com.example.coffee.MainActivity.submitOrder(MainActivity.java:42)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6308)
at android.widget.TextView.performClick(TextView.java:11202)
at android.view.View$PerformClick.run(View.java:23969)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
答案 0 :(得分:0)
输入以下语句:
int price = Integer.parseInt(orderSummaryTextView_andPrice.getText().toString()); // price = the value of textview 2 in int form
在try-catch块中可以进一步调查该问题。这是您尝试在代码中尝试执行的唯一数字解析,并且异常的根本原因是NumberFormatException,而您实际上正在使用
输入字符串:“”
对您的调查发表评论,我会在答案中添加建议:)