2017-04-25 121 views
0

我是一名在Udacity Beginners课程上开发JustJava应用程序的新Android开发人员。使用我的手机来调试我的代码,当我点击按钮更新价格时,它会给出错误信息:“不幸的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) 
+0

你在哪里调用这些方法? –

+1

你有没有试过在Android模拟器中运行这个?看起来你的设备不知道'android.icu.text.NumberFormat'是什么。您可以从堆栈跟踪中看到:'java.lang.NoClassDefFoundError:android.icu.text.NumberFormat'。根据文档,这只是在API级别24添加:https://developer.android.com/reference/android/icu/text/NumberFormat.html – infl3x

+0

好吧。谢谢@davidxxx – JEDiTech

回答

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

+0

哇。好吧。我使用Android 4.4版的金立M3手机。是否可以升级到Android 7.0? – JEDiTech

+0

对不起,不知道,现在你可以使用模拟器。 – davidxxx

相关问题