超级的超级简单问题,有人能帮帮忙吗

Android 码拜 7年前 (2017-04-15) 1064次浏览
按照教程开发了一个点咖啡程序,结果每当点击按钮,程序就会崩坏,真是……什么鬼啊!
布局:
<……省略开头结尾
<TextView
android:id=”@+id/Quantity”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:paddingBottom=”16dp”
android:text=”Quantity”
android:textAllCaps=”true” />
<TextView
android:id=”@+id/Quantity_text_view”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”0″
android:textColor=”#000000″
android:textSize=”16sp”
android:textStyle=”bold” />
<TextView
android:id=”@+id/Price”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Price”
android:textAllCaps=”true”
android:paddingTop=”16dp”
android:paddingBottom=”16dp”
/>
<TextView
android:id=”@+id/Price_text_view”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”$0″
android:textColor=”#000000″
android:textSize=”16sp”
android:textStyle=”bold” />
<Button
android:id=”@+id/Order”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:paddingTop=”16dp”
android:text=”ORDER”
android:onClick=”submitOrder”/>
……>
主程序:
……省略导入……
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view){
display(1);
displayPrice(1*5);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number){
TextView quantitytextview=(TextView)findViewById(R.id.Quantity_text_view);
quantitytextview.setText(number);
}
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number){
TextView pricetextview=(TextView)findViewById(R.id.Price_text_view);
pricetextview.setText(NumberFormat.getCurrencyInstance().format(number));
}
}
执行结果:
未点击order按钮前:
……The application may be doing too much work on its main thread……
……too much output to process……
点击order按钮后:
FATAL EXCEPTION: main
Process: com.example.neo.justjava, PID: 18610
java.lang.IllegalStateException: Could not execute method for android:onClick

超级的超级简单问题,有人能帮帮忙吗
完全想不通了,明明之前没有displayPrice时还能运行的~~~
解决方案

10

本人之前遇到过相似的情况是原因是TextView的setText不能输入数字,你试试quantitytextview.setText(number. + “”);

10

引用:

本人之前遇到过相似的情况是原因是TextView的setText不能输入数字,你试试quantitytextview.setText(number. + “”);

是TextView里面有一个setText(int resId)的方法,参数是一个string的资源引用,系统会找你传递的资源Id,你传的参数不是一个Id,找不到自然报错

10

TextView的setText()可以接收多种参数,具体有
public final void setText(CharSequence text)
public final void setText(char[] text, int start, int len)
public final void setText(@StringRes int resid)
public final void setText(@StringRes int resid, BufferType type)
前两个直接传入字符串,后两个传入int类型的Resource Id
传入数字要先转换成String

10

 quantitytextview.setText(number);改成 quantitytextview.setText(number+“”);

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明超级的超级简单问题,有人能帮帮忙吗
喜欢 (0)
[1034331897@qq.com]
分享 (0)