2013-05-05 50 views
0

我有九個按鈕,每個按鈕都以數字形式打到EditText,有點像計算器。我只是將其作爲一個簡單的項目來完成,但我一直在接收這些NullPointerExeptions。如果我只用預先製作的onCreate()方法運行它,它似乎工作,但一旦我開始在類聲明下聲明變量,它會拋出一些異常。任何幫助表示讚賞。謝謝。在啓動期間獲取NullPointerException

public class MainActivity extends Activity implements OnClickListener { 

EditText display = (EditText)findViewById(R.id.numdisplay); 
Button clearbtn = (Button)findViewById(R.id.clear); 
Button ninenum = (Button)findViewById(R.id.nine); 
Button eightnum = (Button)findViewById(R.id.eight); 
Button sevennum = (Button)findViewById(R.id.seven); 
Button sixnum = (Button)findViewById(R.id.six); 
Button fivenum = (Button)findViewById(R.id.five); 
Button fournum = (Button)findViewById(R.id.four); 
Button threenum = (Button)findViewById(R.id.three); 
Button twonum = (Button)findViewById(R.id.two); 
Button onenum = (Button)findViewById(R.id.one); 
Button enterbtn = (Button)findViewById(R.id.enter); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    onclicks(); //setting all the onclick listeners 
} 

public void onclicks() { //just setting onclick listeners, so it doesn't bloat up the oncreate method 
    clearbtn.setOnClickListener(this); 
    ninenum.setOnClickListener(this); 
    eightnum.setOnClickListener(this); 
    sevennum.setOnClickListener(this); 
    sixnum.setOnClickListener(this); 
    fivenum.setOnClickListener(this); 
    fournum.setOnClickListener(this); 
    threenum.setOnClickListener(this); 
    twonum.setOnClickListener(this); 
    onenum.setOnClickListener(this); 
} 

@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 void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.clear: /** More switch cases will be put here*/ 
      display.setText(""); 
      break; 
} 
} 
} 

回答

1

將此代碼

ditText display = (EditText)findViewById(R.id.numdisplay); 
Button clearbtn = (Button)findViewById(R.id.clear); 
Button ninenum = (Button)findViewById(R.id.nine); 
Button eightnum = (Button)findViewById(R.id.eight); 
Button sevennum = (Button)findViewById(R.id.seven); 
Button sixnum = (Button)findViewById(R.id.six); 
Button fivenum = (Button)findViewById(R.id.five); 
Button fournum = (Button)findViewById(R.id.four); 
Button threenum = (Button)findViewById(R.id.three); 
Button twonum = (Button)findViewById(R.id.two); 
Button onenum = (Button)findViewById(R.id.one); 
Button enterbtn = (Button)findViewById(R.id.enter); 

onCreate()內後setContentView

因爲

,你不會得到你的意見,直到您setContentView引用onCreate(),所以當你試圖要獲得onCreate()以外的參考,您將獲得null參考,並訪問null會給你NullPointerException

聲明這樣

Button clearbtn ,ninenum ,eightnum ,sevennum ,sixnum ,fivenum ,fivenum ,fournum ,threenum ,twonum ,onenum ,enterbtn ; 
EditText display; 

而在的onCreate()

setContentView(R.layout.activity_main); 

display = (EditText)findViewById(R.id.numdisplay); 
clearbtn = (Button)findViewById(R.id.clear); 
ninenum = (Button)findViewById(R.id.nine); 
eightnum = (Button)findViewById(R.id.eight); 
sevennum = (Button)findViewById(R.id.seven); 
sixnum = (Button)findViewById(R.id.six); 
fivenum = (Button)findViewById(R.id.five); 
fournum = (Button)findViewById(R.id.four); 
threenum = (Button)findViewById(R.id.three); 
twonum = (Button)findViewById(R.id.two); 
onenum = (Button)findViewById(R.id.one); 
enterbtn = (Button)findViewById(R.id.enter); 
0

Findviewbyid將返回你的setContentView該佈局之後。你正在做的是試圖在課堂負荷上進行回顧。

嘗試增加

EditText display = (EditText)findViewById(R.id.numdisplay); 
Button clearbtn = (Button)findViewById(R.id.clear); 
Button ninenum = (Button)findViewById(R.id.nine); 
Button eightnum = (Button)findViewById(R.id.eight); 
Button sevennum = (Button)findViewById(R.id.seven); 
Button sixnum = (Button)findViewById(R.id.six); 
Button fivenum = (Button)findViewById(R.id.five); 
Button fournum = (Button)findViewById(R.id.four); 
Button threenum = (Button)findViewById(R.id.three); 
Button twonum = (Button)findViewById(R.id.two); 
Button onenum = (Button)findViewById(R.id.one); 
Button enterbtn = (Button)findViewById(R.id.enter); 

setContentView聲明

0

setContentview()用來設置活動內容,以明確的看法,所以引用到一個視圖之前,你不能在使用的控件。

嘗試下面的代碼

public class MainActivity extends Activity implements OnClickListener { 
EditText display; 
Button clearbtn,ninenum,eightnum,sevennum,sixnum,fivenum,fournum,threenum,twonum,onenum,enterbtn; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    display = (EditText)findViewById(R.id.numdisplay); 
    onclicks(); //setting all the onclick listeners 
} 

public void onclicks() { //just setting onclick listeners, so it doesn't bloat up the oncreate method 
    clearbtn.setOnClickListener(this); 
    ninenum.setOnClickListener(this); 
    eightnum.setOnClickListener(this); 
    sevennum.setOnClickListener(this); 
    sixnum.setOnClickListener(this); 
    fivenum.setOnClickListener(this); 
    fournum.setOnClickListener(this); 
    threenum.setOnClickListener(this); 
    twonum.setOnClickListener(this); 
    onenum.setOnClickListener(this); 
} 

@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 void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.clear: /** More switch cases will be put here*/ 
      display.setText(""); 
      break; 
} 
} 
} 

希望這會有所幫助。

+0

它仍然拋出異常。這是最初發生的事情,我不知道爲什麼。我確保將onclicklisteners和edittext放在SetContentView部分之後。 – ThatGuyThere 2013-05-05 16:23:40

+0

@ThatGuyThere:嘗試用上面的代碼替換你的完整代碼......如果問題仍然存在,請將你的日誌放在這裏 – 2013-05-05 17:32:58