【问题标题】:Android must implement the inherited abstract methodAndroid必须实现继承的抽象方法
【发布时间】:2015-06-10 14:53:50
【问题描述】:

我已经下载了这个功能很好的项目,但是当我把这个功能复制到我的项目中时,我得到了错误:

类型new AsyncHttpResponseHandler(){}必须实现继承的抽象方法AsyncHttpResponseHandler.onSuccess(int, Header[], byte[])

new AsyncHttpResponseHandler(){} 类型的方法 onSuccess(String) 必须覆盖或实现超类型方法

new AsyncHttpResponseHandler(){} 类型的方法 onFailure(int, Throwable, String) 必须覆盖或实现超类型方法

我尝试了this question 的所有提示,但没有任何帮助。任何可能的解决方案?

public void syncSQLiteMySQLDB(){
    //Create AsycHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    ArrayList<HashMap<String, String>> userList =  controller.getAllUsers();
    if(userList.size()!=0){
        if(controller.dbSyncCount() != 0){
            prgDialog.show();
            params.put("usersJSON", controller.composeJSONfromSQLite());
            client.post("http://techkeg.tk/sqlitemysqlsync/insertuser.php",params ,new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    System.out.println(response);
                    prgDialog.hide();
                    try {
                        JSONArray arr = new JSONArray(response);
                        System.out.println(arr.length());
                        for(int i=0; i<arr.length();i++){
                            JSONObject obj = (JSONObject)arr.get(i);
                            System.out.println(obj.get("id"));
                            System.out.println(obj.get("status"));
                            controller.updateSyncStatus(obj.get("id").toString(),obj.get("status").toString());
                        }
                        Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Throwable error, String content) {
                    prgDialog.hide();
                    if(statusCode == 404){
                        Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    }else if(statusCode == 500){
                        Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }else{
            Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show();
        }
    }else{
            Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show();
    }
}

【问题讨论】:

  • 你下载了什么项目?您是否在班级中导入了正确的班级?
  • 也许您使用的是 github.com/loopj/android-async-http 的不同版本,AsyncHttpResponseHandler 似乎来自该版本。因此编译器需要不同的方法签名
  • 是的,我使用了错误版本的 lib,但我认为问题出在某些版本的 java 编译器上。

标签: java android inheritance


【解决方案1】:

根据您的错误和API,您不能为重写的方法创建新签名,您的方法必须具有与超类/接口相同的签名:

Check JLS (§8.4.2)

如果 [...] 签名为 override-equivalent [...] 的方法具有不同的返回类型或不兼容的 throws 子句,则会出现编译时错误。

在您的情况下,此签名必须为:

public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
     // Successfully got a response
 }

public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
{
     // Response failed :(
}

不是:

public void onSuccess(String response) {
public void onFailure(int statusCode, Throwable error, String content) {

Resuming.... 在AsyncHttpResponseHandler API 中声明您的上述方法,并添加它们以满足您的需求。

【讨论】:

  • 哦,谢谢!我刚刚注意到我使用了 1.4.6 版的 android-async-http 并且在下载的项目中是 1.4.4 版。这就是为什么一个项目有效而另一个无效的原因......
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
相关资源
最近更新 更多