我的应用程序崩溃了。不幸的是应用已经停止

时间:2017-04-25 09:41:07

标签: java android xml

我是一名新的Android开发人员,正在研究Udacity Beginners课程中的JustJava应用程序。使用我的手机调试我的代码,当我点击按钮更新价格时,它会给出错误:“不幸的是,JustJava已经停止了。”

的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错误:

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)

1 个答案:

答案 0 :(得分:3)

错误相当具体。根本原因是在堆栈跟踪的末尾:

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)

类加载器可能找不到您在应用中使用的android.icu.text.NumberFormat类定义 您应该检查手机上安装的Android版本是否与仅从API 24(Android 7.0)开始提供的此类兼容。

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