在启动期间获取NullPointerException

在启动期间获取NullPointerException

问题描述:

我有九个按钮,每个按钮都以数字形式打到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; 
} 
} 
} 

将此代码

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); 

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声明

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