我正在通过Udacity的Android Basics:用户输入课程。以前一切都运行良好,但我已经打开了项目,现在我得到了重复的运行时崩溃。我从以前的工作中没有改变任何东西(我知道)。
每当我使用increment()和decrement()时都会发生崩溃。我通过调试器并收到一条IDE错误的通知,所以我不确定这是Android Studio的问题还是我自己的错误。
我很难理解如何理解这个问题,并希望得到一些帮助。如果我需要在此帖子中添加更多数据,请与我们联系。
MainActivity.java,Logcat,build.gradle,activity_main.xml的副本如下。
MainActivity.java
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int quantity = 0;
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
int price = calculatePrice();
String priceMessage = createOrderSummary();
displayMessage(priceMessage);
}
/**
* This method is called when the '+' button is clicked.
*/
public void increment(View view) {
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the '-' button is clicked.
*/
public void decrement(View view) {
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* This method displays an order summary after hitting the 'order' button
*/
private String createOrderSummary() {
String nameLine = "Name: Kaptain Kunal";
String quantityLine = "Quantity: " + quantity;
String totalLine = "Total: $ " + calculatePrice();
String thankyouLine = "Thank you!";
String orderSummary = nameLine + "\n" + quantityLine + "\n" + totalLine + "\n" + thankyouLine;
return orderSummary;
}
/**
* This method calculates the total cost of the order
* @return correctPrice = quantity of coffee * cost of per cup
*/
private int calculatePrice() {
int price = quantity * 5;
return price;
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_tex_view);
orderSummaryTextView.setText(message);
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(number);
}
}
logcat的
04-29 16:35:32.077 6391-6456/com.example.android.justjava E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
04-29 16:38:13.239 6391-6391/com.example.android.justjava E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.android.justjava, PID: 6391
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:339)
at android.widget.TextView.setText(TextView.java:5496)
at com.example.android.justjava.MainActivity.displayQuantity(MainActivity.java:88)
at com.example.android.justjava.MainActivity.increment(MainActivity.java:44)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(模块)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_toppings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toppings"
android:textAllCaps="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_whippedCream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
/>
<TextView
android:id="@+id/tv_whippedCream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whipped cream"
android:layout_marginLeft="24dp"
android:textSize="16sp"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity"
android:textAllCaps="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:onClick="decrement"
android:layout_marginLeft="16dp"
/>
<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:layout_below="@+id/tv_quantity"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
/>
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:onClick="increment"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order Summary"
android:textAllCaps="true"
android:layout_below="@+id/quantity_text_view"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
/>
<TextView
android:id="@+id/order_summary_tex_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$0"
android:textColor="#000000"
android:textSize="16sp"
android:layout_below="@+id/tv_price"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="order"
android:textAllCaps="true"
android:textColor="#000000"
android:layout_below="@+id/price_text_view"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:onClick="submitOrder"
/>
</LinearLayout>