在启动期间获取NullPointerException

时间:2013-05-05 06:50:15

标签: android nullpointerexception

我有九个按钮,每个按钮都以一个数字打到EditText,有点像计算器。我只是将它作为一个简单的项目,但我一直收到这些NullPointerExeptions。如果我只使用预先制作的onCreate()方法运行它,它似乎工作但是一旦我开始在类声明下声明变量,它会抛出一些异常。任何帮助表示赞赏。谢谢。

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

3 个答案:

答案 0 :(得分: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内部

由于

你不会在setContentViewonCreate()获取你的观点参考,所以当你试图在onCreate()之外获得参考时,你会得到{ {1}}引用,访问null会给您null

像这样声明

NullPointerException

在onCreate()

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

答案 1 :(得分:0)

只有在setcontentView布局之后,Findviewbyid才会返回。你正在做的是试图在课堂上加载。

尝试添加

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陈述

之后

答案 2 :(得分: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;
}
}
}

希望这会有所帮助。