我正在尝试从Dialog edittext中设置Handler中的值,但是我收到错误。代码是这样的:
private Button btnstart;
private Button btnstop;
TimePicker myTimePicker;
final static int RQS_1 = 1;
TimePickerDialog timePickerDialog;
Context context;
private int n = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnstart = (Button)findViewById(R.id.btnstart);
btnstart.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
LayoutInflater l = LayoutInflater.from(MainActivity.this);
View dview = l.inflate(R.layout.dialog, null);
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setView(dview);
final EditText edit = (EditText) dview.findViewById(R.id.edit);
String e = edit.getText().toString();
n = Integer.parseInt(e);
ad.setCancelable(false)
.setTitle("Imposta i Secondi")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
Toast.makeText(getApplicationContext(), "Start",Toast.LENGTH_LONG).show();
}
}, n);
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertD = ad.create();
alertD.show();
}
});
n
值,通常可以是3000,4000,5000或以ms为单位的任何其他值作为整数值。所以我在整数中转换了edittext的编辑值,但我在logcat中得到一个错误:
第Invalid int ""
行n = Integer.parseInt(e);
这里有什么问题?
答案 0 :(得分:1)
您需要将这两行代码从现在的位置移到DialogInterface.OnClickListener的onClick处理程序中。
String e = edit.getText().toString();
n = Integer.parseInt(e);
private Button btnstart;
private Button btnstop;
TimePicker myTimePicker;
final static int RQS_1 = 1;
TimePickerDialog timePickerDialog;
Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnstart = (Button)findViewById(R.id.btnstart);
btnstart.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
LayoutInflater l = LayoutInflater.from(MainActivity.this);
View dview = l.inflate(R.layout.dialog, null);
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setView(dview);
final EditText edit = (EditText) dview.findViewById(R.id.edit);
ad.setCancelable(false)
.setTitle("Imposta i Secondi")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
String e = edit.getText().toString();
int n = Integer.parseInt(e);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
Toast.makeText(getApplicationContext(), "Start",Toast.LENGTH_LONG).show();
}
}, n);
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertD = ad.create();
alertD.show();
}
});
答案 1 :(得分:0)
您的EditText
在什么时候填充?这是在一个创建事件中,所以你似乎试图从空字符串中提取一个整数?
答案 2 :(得分:0)
int value;
try{
value = Integer.parseInt(stringValue);
}(Execption e){
value = 0; // If edittext contains letters, space, etc
}
答案 3 :(得分:0)
首先,您应该使用try catch来格式化数字。
String e = edit.getText().toString().trim();
if(e!=null && !e.isEmpty())
{
int n ;
try {
n = Integer.valueOf(e);
} catch (NumberFormatException e) {
e.printStackTrace();
n = 0;
}
}
希望它有所帮助。