Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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中的错误publishprogress_Java - Fatal编程技术网

Java 如何解决asynctask中的错误publishprogress

Java 如何解决asynctask中的错误publishprogress,java,Java,我是android新手。我有一个关于asynctaskbackground的简单代码。它不会编译。我在某行有问题。在doinBackground方法中。我的代码是: public class MainActivity extends ActionBarActivity { ProgressDialog progressBar; int progressincr = 1; @Override protected void onCreate(Bundle saved

我是android新手。我有一个关于asynctaskbackground的简单代码。它不会编译。我在某行有问题。在doinBackground方法中。我的代码是:

public class MainActivity extends ActionBarActivity {
    ProgressDialog progressBar;
    int progressincr = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button startTask = (Button) findViewById(R.id.startTaskButton);
        startTask.setOnClickListener(startTaskListener);
    }
    private OnClickListener startTaskListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = getApplicationContext();
            progressBar = new ProgressDialog(v.getContext());
            BackgroundTask testBackgroundTask = new BackgroundTask();
            testBackgroundTask.execute(context);

            CharSequence text = getString(R.string.mainThreadRunning);
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }
    };

    private class BackgroundTask extends AsyncTask < Context, integer, string>{
        @Override
        protected void onPreExecute() {
            CharSequence message = getString(R.string.progressMessage);
            progressBar.setCancelable(true);
            progressBar.setMessage(message);
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();
        };

        @Override
        protected String doInBackground(Context... params) {
            for (int i = 0; i < 100; i = i + progressincr) {
                try { Thread.sleep(100);}

                 catch (InterruptedException e) {
                    e.printStackTrace();}

                publishProgress(progressincr);
                if(isCancelled()) break;

            }
            return getString(R.string.backgroundTaskcompleted);

        }
        protected void onProgressUpdate(Integer...values){
            progressBar.incrementProgressBy(progressincr);

        }
        protected void onPostExecute(String result){
            progressBar.dismiss();

            Context context = getApplicationContext();
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context,result, duration);
            toast.show();
        }

    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
公共类MainActivity扩展了ActionBarActivity{
ProgressDialog progressBar;
int progressincr=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮startTask=(按钮)findViewById(R.id.startTaskButton);
setOnClickListener(startTaskListener);
}
私有OnClickListener startTaskListener=新OnClickListener(){
@凌驾
公共void onClick(视图v){
Context=getApplicationContext();
progressBar=newprogressdialog(v.getContext());
BackgroundTask testBackgroundTask=新的BackgroundTask();
testBackgroundTask.execute(上下文);
CharSequence text=getString(R.string.mainThreadRunning);
int duration=Toast.LENGTH\u LONG;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
}
};
私有类BackgroundTask扩展AsyncTask<上下文,整数,字符串>{
@凌驾
受保护的void onPreExecute(){
CharSequence message=getString(R.string.progressMessage);
progressBar.setCancelable(真);
progressBar.setMessage(消息);
progressBar.setProgressStyle(ProgressDialog.STYLE_水平);
progressBar.setProgress(0);
设置最大进度条(100);
progressBar.show();
};
@凌驾
受保护字符串doInBackground(上下文…参数){
对于(int i=0;i<100;i=i+progressincr){
试试{Thread.sleep(100);}
捕捉(中断异常e){
e、 printStackTrace();}
出版进度(progressincr);
如果(isCancelled())中断;
}
返回getString(R.string.backgroundTaskcompleted);
}
受保护的void onProgressUpdate(整型…值){
progressBar.incrementProgressBy(progressincr);
}
受保护的void onPostExecute(字符串结果){
progressBar.disclose();
Context=getApplicationContext();
int duration=Toast.LENGTH\u LONG;
Toast Toast=Toast.makeText(上下文、结果、持续时间);
toast.show();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
错误是:

  ***The method publishProgress(R.integer...) in the type       AsyncTask<Context,R.integer,R.string> is not applicable for the arguments (int) MainActivity.java   /AsyncTaskBackg/src/com/example/asynctaskbackg  line 67 Java Problem***
***类型AsyncTask中的publishProgress(R.integer…)方法不适用于参数(int)MainActivity.java/AsyncTaskBackg/src/com/example/AsyncTaskBackg第67行java问题***
以及:

***说明资源路径位置类型
返回类型与AsyncTask.doInBackground(Context[])MainActivity.java/AsyncTaskBackg/src/com/example/AsyncTaskBackg line 60 java问题不兼容***

我怎样才能修好它们??是什么原因造成的?

您的泛型类型搞错了。
必须是
,因为泛型只引用,而不引用值类型。另外,
integer
string
等类型也不存在。原语类型是
int
,对应的包装类是
Integer
。而且
字符串
类型没有基本变体,因此它总是以大写字母开头。

中定义的这三种类型定义了返回类型或参数,引用如下:
异步任务由3种泛型类型定义,称为Params、Progress和Result。
,但是如果您修复了类型名称,您将已经按照正确的顺序将它们导入。

在导入过程中,我将导入其中的所有包。
***Description  Resource    Path    Location    Type
The return type is incompatible with AsyncTask<Context,R.integer,R.string>.doInBackground(Context[])    MainActivity.java   /AsyncTaskBackg/src/com/example/asynctaskbackg  line 60 Java Problem***