简体   繁体   中英

My App crashed. Unfortunately app has stopped

I'm a new Android developer working on the JustJava app on Udacity Beginners course. Using my phone to debug my code, when I clicked on the button to update price it gives the error: "Unfortunately JustJava has stopped".

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.justjava">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="justjava"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER"     />
            </intent-filter>
        </activity>
    </application>

MainActivity.java

import android.icu.text.NumberFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
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);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        display(2);
        displayPrice(2 * 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));
    }
 }

activity_main.xml

<?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="com.example.android.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="quantity"
        android:textAllCaps="true"
        android:padding="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:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:textColor="@android:color/black"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:textAllCaps="true"
        android:padding="16dp" />

    <TextView
        android:id="@+id/price_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NGN0"
        android:textSize="16sp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:textColor="@android:color/black"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ORDER"
        android:layout_margin="16dp"
        android:onClick="submitOrder"/>


</LinearLayout>

Logcat error:

04-25 09:22:04.944 449-449/com.example.android.justjava E/AndroidRuntime: FATAL EXCEPTION: main                                                                      Process: com.example.android.justjava, PID: 449
                                                                      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:4470)
                                                                          at android.view.View$PerformClick.run(View.java:18773)
                                                                          at android.os.Handler.handleCallback(Handler.java:808)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                          at android.os.Looper.loop(Looper.java:193)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5341)
                                                                          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:830)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
                                                                          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:4470) 
                                                                          at android.view.View$PerformClick.run(View.java:18773) 
                                                                          at android.os.Handler.handleCallback(Handler.java:808) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:103) 
                                                                          at android.os.Looper.loop(Looper.java:193) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5341) 
                                                                          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:830) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 
                                                                       Caused by: java.lang.NoClassDefFoundError: android.icu.text.NumberFormat
                                                                          at com.example.android.justjava.MainActivity.displayPrice(MainActivity.java:49)
                                                                          at com.example.android.justjava.MainActivity.submitOrder(MainActivity.java:33)
                                                                          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:4470) 
                                                                          at android.view.View$PerformClick.run(View.java:18773) 
                                                                          at android.os.Handler.handleCallback(Handler.java:808) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:103) 
                                                                          at android.os.Looper.loop(Looper.java:193) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5341) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)

The error is rather specific. The root cause is at the end of the stacktrace :

Caused by: java.lang.NoClassDefFoundError: android.icu.text.NumberFormat
at com.example.android.justjava.MainActivity.displayPrice(MainActivity.java:49)
at com.example.android.justjava.MainActivity.submitOrder(MainActivity.java:33)

The classloader could have not found the android.icu.text.NumberFormat class definition you are using in your app.
You should check that the Android version installed on your phone is compatible with this class that is available only since the API 24 (Android 7.0).

https://developer.android.com/reference/android/icu/text/NumberFormat.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM