繁体   English   中英

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

[英]My App crashed. Unfortunately app has stopped

我是一名新的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)

该错误相当具体。 根本原因是在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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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