[英]Android: app crashing after button click
我是android的新手,我使用的是Android Studio 2.2进行编码,并且尝试构建我的第一个应用程序,基本上该应用程序并没有做很多事情或现在做不到,然后在为我的onClick
方法添加了price方法后应用程序,只要我单击按钮,应用程序就会崩溃。
在下面,我提供了错误消息和我自己的代码。
我从Android Studio获得的错误消息是从logcat视图复制的:
10-06 09:46:00.338 30789-30789/com.example.abdulkarim.justjava E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdulkarim.justjava, PID: 30789
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4660)
at android.view.View$PerformClick.run(View.java:19445)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4660)
at android.view.View$PerformClick.run(View.java:19445)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:1431)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4954)
at com.example.abdulkarim.justjava.MainActivity.display(MainActivity.java:33)
at com.example.abdulkarim.justjava.MainActivity.submit(MainActivity.java:24)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4660)
at android.view.View$PerformClick.run(View.java:19445)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
10-06 09:47:13.823 30789-30789/? I/Process: Sending signal. PID: 30789 SIG: 9
这是我的MainActivity.java代码
package com.example.abdulkarim.justjava;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
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 submit(View view){
int numberOfCoffees = 2;
display(numberOfCoffees);
displayPrice(numberOfCoffees * 10);
}
/**
* This method displays the given quantity 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));
}
}
这是来自main_activity.xml的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.abdulkarim.justjava.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity"
android:textAllCaps="true"
android:textSize="16sp"
android:textColor="@android:color/darker_gray"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16sp"
android:textColor="@android:color/black"
android:layout_marginBottom="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textAllCaps="true"
android:textSize="16sp"
android:textColor="@android:color/darker_gray"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$0"
android:textSize="16sp"
android:textColor="@android:color/black"
android:layout_marginBottom="16dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:textAllCaps="true"
android:textSize="16sp"
android:onClick="submit"/>
</LinearLayout>
请帮助我在代码中找到错误。
您不能直接为您的Textview设置整数。 仅允许使用String,因此更新您的方法即可使用:
/**
* This method displays the given quantity 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)+"");
}
问题在于显示功能的以下行,因为您不能通过调用setText方法而是将字符串设置为TextView来将整数设置为TextView。
quantityTextView.setText(number);
请用这个来改变这行
quantityTextView.setText(number+"");
你的问题只在
private void display(int number){
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(number);
}
在这里您不能直接设置号码。 野兔只字符串除外。 您可以添加带数字的空字符串来解决此问题...
quantityTextView.setText(number+"");
和
NumberFormat.getCurrencyInstance().format(number)
返回字符串,因此无需添加任何内容...
settext()方法接受字符串作为参数。 您不能直接将任何其他数据类型传递给它。 要在textview中显示任何其他数据类型,您必须使用“ +”运算符将其附加到字符串中:
像:[textview] .setText(“” + integerValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.