我参与了一项活动:
...
super.onCreate(savedInstanceState);
setContentView(R.layout.configure);
edit1=(EditText) findViewById(R.id.edit1);
edit2=(EditText) findViewById(R.id.edit2);
edit11=edit1.getText().toString();
edit22=edit2.getText().toString();
btn=(View) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
Intent i=new Intent(this,selection.class);
i.putExtra("code_value",edit11);
i.putExtra("time_value",edit22);
startActivity(i);
break;
}
并在选择活动中:
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
String code_value=getIntent().getExtras().getString("code_value");
String time_value=getIntent().getExtras().getString("time_value");
long thetime=Long.parseLong(time_value);
..
// Start service using AlarmManager
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(selection.this, selection.class);
PendingIntent pintent = PendingIntent.getService(selection.this, 0, intent,
0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
thetime, pintent);
其中长“thetime”我想在alarm.setRepeating中使用它。 但它给了我nullpointerexception ..
答案 0 :(得分:2)
您需要从edittext的内部按钮点击而不是活动的onCreate
获取文本
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
// retrieve text here from EditText
edit11=edit1.getText().toString();
edit22=edit2.getText().toString();
Intent i=new Intent(this,selection.class);
i.putExtra("code_value",edit11);
i.putExtra("time_value",edit22);
startActivity(i);
break;
}
答案 1 :(得分:2)
你的问题是你在初始化后立即调用getText()
并且它总是返回空字符串。
您需要在要处理文本的时刻准确调用 getText(),以便将其移至onClick()
方法:
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
String e1 = edit1.getText().toString();
String e2 = edit2.getText().toString();
Intent i=new Intent(this,selection.class);
i.putExtra("code_value", e1);
i.putExtra("time_value", e2);
startActivity(i);
break;
}