2014-07-19 30 views
1

我有一個非常簡單的應用程序,兩個buttons和一個text。我按一個,text上的數字就會上升。另一個使數量減少。這是我的代碼。應用程序一直停止,我不知道發生了什麼

package com.bryantpc.itemcounter; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

import com.bryantpc.itemcounter.R; 

public class IC_MAIN extends ActionBarActivity { 


private int Amount1 = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button Button1 = (Button)findViewById(R.id.button1);  
    final Button Button2 = (Button)findViewById(R.id.button2);  

    final TextView TextView1 = (TextView)findViewById(R.id.textView1);  

    Button1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) {  
      Amount1++; 
      TextView1.setText(Amount1); 
     } 
    }); 

    Button2.setOnClickListener(new OnClickListener() { 
     public void onClick (View v) { 
      Amount1--; 
      TextView1.setText(Amount1); 
     } 
    }); 

每當我按下其中一個按鈕,應用程序就停止工作。任何人都知道發生了什麼事? P.S我沒有LogCat,因爲我的模擬器不工作我在手機上運行APK。

+0

http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve命名類和變量 - 這 – nhaarman

回答

2

你不能做TextView1.setText(Amount1),除非這是一個資源ID,在這種情況下,它不是。設置文字時使用TextView1.setText(String.valueOf(Amount1))

順便說一句,你應該根據自己像https://source.android.com/source/code-style.html

+0

那麼我應該改變'TextView1.setText(Amount1);'來? – user7777777

+0

'TextView1.setText(String.valueOf(Amount1))' – Dororo

+0

非常感謝!只是最後一個問題......你如何將值限制在0或以上? – user7777777

相關問題