通过AsyncTask传递参数 - android

时间:2014-04-20 23:19:06

标签: java android android-asynctask

我正在尝试通过AsyncTask传递一个整数,以便在点击按钮时在doInBackground中使用。

这是我传递的参数

public void go(View view) {

    EditText input = (EditText)findViewById(R.id.txtInput);

    int downloads = Integer.parseInt(input.toString());

    ProcessTask myTask = new ProcessTask();

    //5000 - number of Ms to simulate download

    myTask.execute(5000, downloads);

}

然后在这里我尝试使用我传递的相同值

    private class ProcessTask extends AsyncTask<Integer, Integer, Void>{

@Override
protected Void doInBackground(Integer... params) {
    // TODO Auto-generated method stub

    Integer myD = params[1];


    for(int i = 0; i<myD; i++){
        try{
            Thread.sleep(params[0]);
            publishProgress(i+1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    return null;
}

我一直收到以下错误:

    04-20 19:10:43.198: E/AndroidRuntime(4363): FATAL EXCEPTION: main
04-20 19:10:43.198: E/AndroidRuntime(4363): java.lang.IllegalStateException: Could not execute method of the activity
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.view.View$1.onClick(View.java:3633)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.view.View.performClick(View.java:4240)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.view.View$PerformClick.run(View.java:17721)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.os.Handler.handleCallback(Handler.java:730)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.os.Looper.loop(Looper.java:137)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.reflect.Method.invoke(Method.java:525)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at dalvik.system.NativeStart.main(Native Method)
04-20 19:10:43.198: E/AndroidRuntime(4363): Caused by: java.lang.reflect.InvocationTargetException
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.reflect.Method.invoke(Method.java:525)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at android.view.View$1.onClick(View.java:3628)
04-20 19:10:43.198: E/AndroidRuntime(4363):     ... 11 more
04-20 19:10:43.198: E/AndroidRuntime(4363): Caused by: java.lang.NumberFormatException: Invalid int: "android.widget.EditText{41754410 VFED..CL .F...... 32,110-736,188 #7f080002 app:id/txtInput}"
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.Integer.invalidInt(Integer.java:138)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.Integer.parse(Integer.java:375)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.Integer.parseInt(Integer.java:366)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at java.lang.Integer.parseInt(Integer.java:332)
04-20 19:10:43.198: E/AndroidRuntime(4363):     at com.example.asyncfinalpractice.MainActivity.go(MainActivity.java:30)
04-20 19:10:43.198: E/AndroidRuntime(4363):     ... 14 more

非常感谢任何和所有帮助。我对android很新。

1 个答案:

答案 0 :(得分:1)

您在方法链中错过了getText()来电。

需要input.getText().toString()

线索是logcat输出:

  

无效的int:&#34; android.widget.EditText {...

这绝对是您的输入。

对于号码解析,我建议实际catch异常。这样,您可能忘记过滤的任何输入都不会导致程序崩溃。