Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过AsyncTask传递参数-android_Java_Android_Android Asynctask - Fatal编程技术网

Java 通过AsyncTask传递参数-android

Java 通过AsyncTask传递参数-android,java,android,android-asynctask,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();

我试图通过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;
}

非常感谢您的任何帮助。我对android有点陌生。

在方法链接中缺少一个
getText()
调用

它必须是
input.getText().toString()

线索是logcat输出:

无效int:“android.widget.EditText{

这绝对不是你的意见


对于数字解析,我建议实际上
捕获
异常。这样,您可能忘记过滤的任何输入都不会使程序崩溃。

非常感谢!这是您有时会错过的小事情。
    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