0

我有九个按钮,每个按钮都在 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;
}
}
}
4

3 回答 3

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);
于 2013-05-05T06:52:25.600 回答
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声明后

于 2013-05-05T06:54:52.987 回答
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;
}
}
}

希望这可以帮助。

于 2013-05-05T07:08:52.183 回答