我看过其他主题,但他们根本没有为我工作过。
我是Java和Android开发的新手。我正在尝试登录我的Android应用程序,因此我尝试在线跟踪教程,然后按照我希望的方式进行调整。所以我在整个项目中都遇到了错误,而且它们都是一样的。它在网站上说工作正常,所以我不知道我在做什么。
所以,现在是错误!
Netcheck类中的@Override(除了onPreExecute之外的所有类)都会出现此错误:方法不会覆盖超类中的方法
类'ProcessRegister'必须声明为abstract或在'AsyncTask'中实现抽象方法'doInBackground(params ....)
ProcessRegister类中的@Overide给出错误:Method不会覆盖超类中的方法。除了onPreExecute之外,所有@overrides都会出错。
在查看其他帖子后,它说要确保方法是公开的或受保护的。他们是谁,另一个说要检查拼写。但是,除非我遗漏了一些东西,否则一切看起来都不错。有人可以给我一些帮助!谢谢!
import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.drm.ProcessedData;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import wishlist.com.gimme.library.UserFunctions;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class PasswordReset extends Activity {
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
EditText email;
TextView alert;
Button resetpass;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passwordreset);
Button login = (Button) findViewById(R.id.bktolog);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Login.class);
startActivityForResult(myIntent, 0);
finish();
}
});
email = (EditText) findViewById(R.id.forpas);
alert = (TextView) findViewById(R.id.alert);
resetpass = (Button) findViewById(R.id.respass);
resetpass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NetAsync(view);
}
});}
private class NetCheck extends AsyncTask
{
private ProgressDialog nDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(PasswordReset.this);
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected Boolean doInBackground(String... args){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th){
if(th == true){
nDialog.dismiss();
new ProcessRegister().execute();
}
else{
nDialog.dismiss();
alert.setText("Error in Network Connection");
}
}
}
protected class ProcessRegister extends AsyncTask {
private ProgressDialog pDialog;
String forgotpassword;
@Override
protected void onPreExecute() {
super.onPreExecute();
forgotpassword = email.getText().toString();
pDialog = new ProgressDialog(PasswordReset.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.forPass(forgotpassword);
return json;
}
protected void onProgressUpdate(Integer... args) {
}
@Override
protected void onPostExecute(JSONObject json) {
/**
* Checks if the Password Change Process is sucesss
**/
try {
if (json.getString(KEY_SUCCESS) != null) {
alert.setText("");
String res = json.getString(KEY_SUCCESS);
String red = json.getString(KEY_ERROR);
if(Integer.parseInt(res) == 1){
pDialog.dismiss();
alert.setText("A recovery email is sent to you, see it for more details.");
}
else if (Integer.parseInt(red) == 2)
{ pDialog.dismiss();
alert.setText("Your email does not exist in our database.");
}
else {
pDialog.dismiss();
alert.setText("Error occured in changing Password");
}
}}
catch (JSONException e) {
e.printStackTrace();
}
}}
public void NetAsync(View view){
new NetCheck().execute();
}
}
答案 0 :(得分:1)
如果你看the JavaDocs for AsyncTask
,你会看到它表示为:
android.os.AsyncTask<Params, Progress, Result>
您将看到示例DownloadFilesTask
声明为:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long>
您的实现缺少泛型的数据类型。
为此,改变:
private class NetCheck extends AsyncTask
为:
private class NetCheck extends AsyncTask<String, Void, Boolean>
由于doInBackground()
有一个String...
参数(第一种数据类型),因此我们没有调用publishProgress()
(第二种数据类型Void
,因为我们没有使用它),并返回Boolean
(第三种数据类型)。
同样,改变:
protected class ProcessRegister extends AsyncTask
为:
protected class ProcessRegister extends AsyncTask<String, Integer, JSONObject>
Integer
是因为您正在实施onProgressUpdate()
作为Integer
,但您似乎没有使用它。