2016-10-06 1 views
0

Je nouveau avec android, j'utilise Android Studio 2.2 pour mon codage et j'essaye de construire ma première application, fondamentalement l'application ne fait pas beaucoup ou maintenant, et après l'ajout d'une méthode de prix à la méthode onClick pour mon application, l'application se bloque chaque fois que je clique sur le bouton. Ci-dessous, j'ai fourni le message d'erreur et mon propre code.Android: app s'écraser après bouton cliquez

Le message d'erreur que je reçois de mon studio android est aussi copié de la vue logcat:

10-06 09:46:00.338 30789-30789/com.example.abdulkarim.justjava E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.abdulkarim.justjava, PID: 30789 
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:4660) 
at android.view.View$PerformClick.run(View.java:19445) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
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:4660) 
at android.view.View$PerformClick.run(View.java:19445) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2 
at android.content.res.Resources.getText(Resources.java:1431) 
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 
at android.widget.TextView.setText(TextView.java:4954) 
at com.example.abdulkarim.justjava.MainActivity.display(MainActivity.java:33) 
at com.example.abdulkarim.justjava.MainActivity.submit(MainActivity.java:24) 
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:4660) 
at android.view.View$PerformClick.run(View.java:19445) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 
10-06 09:47:13.823 30789-30789/? I/Process: Sending signal. PID: 30789 SIG: 9 

Voici mon code MainActivity.java

package com.example.abdulkarim.justjava; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

import java.text.NumberFormat; 


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 submit(View view){ 
     int numberOfCoffees = 2; 
     display(numberOfCoffees); 
     displayPrice(numberOfCoffees * 10); 
    } 

    /** 
    * This method displays the given quantity 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)); 
    } 
} 

et voici mon code de mise en page de main_activity .xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.example.abdulkarim.justjava.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Quantity" 
     android:textAllCaps="true" 
     android:textSize="16sp" 
     android:textColor="@android:color/darker_gray" 
     android:layout_marginBottom="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:textColor="@android:color/black" 
     android:layout_marginBottom="16dp"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Price" 
     android:textAllCaps="true" 
     android:textSize="16sp" 
     android:textColor="@android:color/darker_gray" 
     android:layout_marginBottom="16dp"/> 
    <TextView 
     android:id="@+id/price_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="$0" 
     android:textSize="16sp" 
     android:textColor="@android:color/black" 
     android:layout_marginBottom="16dp"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Order" 
     android:textAllCaps="true" 
     android:textSize="16sp" 
     android:onClick="submit"/> 
</LinearLayout> 

S'il vous plaît aidez-moi à trouver l'erreur dans mon code.

+2

Modifier ce 'quantityTextView.setText (nombre),' 'à quantityTextView.setText (String.valueOf (number).) De même dans d'autres endroits – Raghunandan

Répondre

1

Vous ne pouvez pas définir directement un nombre entier de TextView. Seule chaîne est autorisée si mettre à jour votre méthode et il fonctionnera:

/** 
    * This method displays the given quantity 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)+""); 
    } 
0

La question est en suivant la ligne de la fonction d'affichage parce que vous ne pouvez pas définir entier à TextView en appelant la méthode setText mais comme une chaîne.

quantityTextView.setText(number); 

changer Veuillez cette ligne avec ce

quantityTextView.setText(number+""); 
+0

Veuillez formater votre code. –

0

Votre problème est que dans

private void display(int number){ 
    TextView quantityTextView = (TextView)                    findViewById(R.id.quantity_text_view); 
    quantityTextView.setText(number); 
} 

Vous ne pouvez pas directement numéro défini. Hare seule chaîne exceptée. Vous pouvez ajouter une chaîne vide avec le numéro pour résoudre ce ...

quantityTextView.setText(number+""); 

et

NumberFormat.getCurrencyInstance().format(number) 

retour STRING donc pas besoin d'ajouter quoi que ce soit ...

0

méthode setText() accepte la chaîne en tant que paramètre vous ne pouvez pas lui passer directement un autre type de données. Pour afficher tout autre type de données dans la vue de texte, vous devez l'ajouter à la chaîne en utilisant l'opérateur "+":

Comme: [textview] .setText ("" + integerValue);

  1. Voici deux guillemets (« ») est une chaîne vide
  2. et (+) est un opérateur d'ajouter deux valeurs.
  3. IntegerValue peut être quelque chose comme (1,2,3,4 ....), que vous voulez afficher dans textview