2015-06-16 2 views
-3

Пожалуйста, скажите мне, почему мое приложение не работает. Я сделал приложение для калькулятора кофе. Вот код XMLК сожалению приложение прекратило работать

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:text="@string/disp" 
     android:textColor="@android:color/black" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/add" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:onClick="Add" 
      android:text="@string/add" 
      android:textColor="@android:color/black" 
      android:textSize="30sp" 
      android:textStyle="bold" 
      android:background="#E57373" /> 

     <TextView 
      android:id="@+id/sum" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:text="@string/text" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/sub" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:onClick="Sub" 
      android:text="@string/sub" 
      android:textColor="@android:color/black" 
      android:textSize="30sp" 
      android:textStyle="bold" 
      android:background="#E57373" /> 
    </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:text="@string/price" 
     android:textColor="@android:color/black" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:text="@string/total" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/res" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="@string/res" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/order" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="210sp" 
     android:onClick="Order" 
     android:text="@string/order" 
     android:textColor="@android:color/black" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:textAllCaps="true" 
     android:background="#E57373"/> 

</LinearLayout> 

И это мой Java-код

package com.example.extra; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    public int num=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void Add(View view){ 
     //On click method for Add button 
     TextView text=(TextView)findViewById(R.id.sum); 
     num=num+1; 
     text.setText(num); 
    } 

    public void Sub(View view){ 
     //On click method for Sub button 
     TextView text=(TextView)findViewById(R.id.sum); 
     if(num==0) 
      text.setText(0); 
     else 
     { num=num-1; 
      text.setText(num); 
     } 
    } 

    public void Order(View view){ 
     //On click method for button ORDER 
     TextView text=(TextView)findViewById(R.id.res); 
     text.setText("$" + num*10); 
    } 

} 

Когда я запускать приложение и нажав на кнопку Добавить или суб кнопку, то приложение crashes.But кнопку ORDER работает fine.Пожалуйста, помогите ...

+4

Использование LogCat для изучения трассировки стека Java, связанный с грохотом: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how -can-i-solve-this – CommonsWare

+2

Вы также можете просто отложить ответ и сказать, что он бросает кнопку в TextView. –

+0

@Edward Я не получил то, что вы сказали, пожалуйста, уточните –

ответ

1

Причина, по которой ваше приложение падает, связано с тем, что вы вставляете текстовые объекты. setText() принимает строковую переменную или int, указывающую идентификатор ресурса. В этом случае int не указывает идентификатор ресурса, поэтому он терпит неудачу. Изменение setText() в Sub и Add к этому:

text.setText("" + num); 
+0

Вы можете установить идентификатор ресурса (int) в виде текста. – mbmc

+0

@tibo Как установить идентификатор ресурса (int) в виде текста. ?? –

+0

'text.setText (R.string.my_string); // my_string определяется в strings.xml' – mbmc

Смежные вопросы