2014-02-10 55 views
1

每當我嘗試運行我的程序時,都會收到該錯誤消息。我有一個相當簡單的程序,有兩個按鈕。如果你按下一個按鈕,它會從數字中減去1。如果按下添加按鈕,則會將數字加1。我沒有編譯錯誤,我相當新的android。任何幫助將是偉大的...謝謝!Eclipse(Android) - 不幸的是,HelloWorld已停止工作

這是我的代碼:(主要活動)。

package com.example.helloworld; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView numberText = (TextView) findViewById(R.id.number); 
     numberText.setText(String.valueOf(0)); 

     Button.OnClickListener listenerAdd = new Button.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       TextView numberText = (TextView) findViewById(R.id.number); 
       switch(v.getId()) { 
        case R.id.HomeScreenChangeAdd: { 
         String currText = numberText.getText().toString(); 
         int curNumb = Integer.parseInt(currText); 
         int newNumb = curNumb +1; 
         String newText = String.valueOf(newNumb); 
         numberText.setText(newText); 
         break; 
        } 
       } 
      } 
     }; 

     Button.OnClickListener listenerSub = new Button.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       TextView numberText = (TextView) findViewById(R.id.number); 
       switch(v.getId()) { 
        case R.id.HomeScreenChangeSub: { 
         String currText = numberText.getText().toString(); 
         int curNumb = Integer.parseInt(currText); 
         int newNumb = curNumb -1; 
         String newText = String.valueOf(newNumb); 
         numberText.setText(newText); 
         break; 
        } 
       } 
      } 
     }; 

     ((Button) findViewById(R.id.HomeScreenChangeAdd)).setOnClickListener(listenerAdd); 
     ((Button) findViewById(R.id.HomeScreenChangeSub)).setOnClickListener(listenerSub); 
    } 

    @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; 
    } 
} 
+2

你可以發佈你的logcat錯誤嗎? – GhostDerfel

+0

我收到很多錯誤,但它說主活動 – user1760791

+0

有一個空指針異常這就是問題:D只需添加日誌並指向該行,然後我們就可以幫助您,也可以發佈你的佈局xml – GhostDerfel

回答

0

確保

  • R.id.number,
  • R.id.HomeScreenChangeAdd和
  • R.id.HomeScreenChangeSub

包括在R.layout.activity_main

您的代碼中沒有任何其他代碼可能會在啓動時啓動應用程序

相關問題