-1

我是 Android 编程领域的初学者。我只是想开发一个示例应用程序。这是项目:

public class CalcActivity extends Activity implements OnClickListener{

EditText input = (EditText) findViewById(R.id.input);
Button one = (Button) findViewById(R.id.button1);
TextView out = (TextView) findViewById(R.id.output);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calc);
    //one.setOnClickListener(this);
}
public void onClick(View v){

  input.setText("HI");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calc, menu);
    return true;
}

}
4

2 回答 2

0

尝试这个

public class CalcActivity extends Activity implements OnClickListener {

EditText input ;
Button one ;
TextView out;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calc);
    input = (EditText) findViewById(R.id.input);
    one = (Button) findViewById(R.id.button1);
    one.setOnClickListener(this);
    out = (TextView) findViewById(R.id.output);
    // one.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        input.setText("HI");
        Log.d("button clicked", "buttom");
        break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calc, menu);
    return true;
}

}

于 2013-09-24T08:40:57.513 回答
0

您没有在 Oncreate() 中初始化视图,因此您正在强制关闭,所以像这样在 onCreate 中初始化您的视图

EditText input;
Button one ;
TextView out;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calc);
    input= (EditText) findViewById(R.id.input);
    one = (Button) findViewById(R.id.button1);
    out = (TextView) findViewById(R.id.output);

}
于 2013-09-24T09:26:06.103 回答