0

我正在尝试编写一个应用程序,该应用程序可以访问我的朋友作为笑话创建的网页,用户可以在其中为愚蠢的事情互相给予“奖牌”。一个例子是该网站的所有者给了另一个朋友,我称他为鲍勃一枚名为“密码天才”的奖章,上面写着“忘记了他的麻省理工学院招生密码,一般来说密码很差。”。奖牌以 JSON 格式检索,上面的示例为
[...other medals....,{"medalid":"21","uid":"bob","title":"Password Genius","description":"Forgot his MIT admissions password and is just bad at passwords in general.","givenby":"fred"}, ...other medals....]
. 我想在我的应用程序中使用 AsyncTask 检索这些,但我得到了最奇怪的编译器错误。简而言之,我的代码是

public class MedalMania extends activity
{
    public static final String TAG = "[MedalMania]";
    public void lookupButtonPressed(View view)
    {
        (new GetMedalsTask()).execute(((EditText)R.findViewById(R.id.name_box))getText().toString());
    }
    private class GetMedalsTask extends AsyncTask<String, void, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            Log.v(TAG, "This will print to console");
            return "i dont need to type my real code, even this simplified form wont work";
        }
        @Override
        protected void onPostExecute(String json)
        {
            Log.v(TAG, json);//This wont print to console
        }
    }
}

如果我尝试这样做,我会得到

[javac] Compiling 4 source files to /home/alex/droid_apps/MedalMania/bin/classes
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:59: illegal start of type
[javac]     private class GetMedalsTask extends AsyncTask<String, void, String>
[javac]                                                           ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:59: '{' expected
[javac]     private class GetMedalsTask extends AsyncTask<String, void, String>
[javac]                                                               ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:59: <identifier> expected
[javac]     private class GetMedalsTask extends AsyncTask<String, void, String>
[javac]                                                                       ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: ';' expected
[javac]         protected String doInBackground(String... params)
[javac]                                        ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: not a statement
[javac]         protected String doInBackground(String... params)
[javac]                                         ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: ';' expected
[javac]         protected String doInBackground(String... params)
[javac]                                               ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: not a statement
[javac]         protected String doInBackground(String... params)
[javac]                                                   ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: ';' expected
[javac]         protected String doInBackground(String... params)
[javac]                                                         ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: illegal start of type
[javac]         protected void onPostExecute(String json)
[javac]                   ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: ';' expected
[javac]         protected void onPostExecute(String json)
[javac]                       ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: ')' expected
[javac]         protected void onPostExecute(String json)
[javac]                                            ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: not a statement
[javac]         protected void onPostExecute(String json)
[javac]                                     ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: ';' expected
[javac]         protected void onPostExecute(String json)
[javac]                                                 ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:81: reached end of file while parsing
[javac] }
[javac]  ^
[javac] 14 errors

但是,如果我更换

private class GetMedalsTask extends AsyncTask<String, void, String>

private class GetMedalsTask extends AsyncTask//<String, void, String>

它会编译,但只有 Log.v(TAG, "this will print to console"); 实际上打印,而不是Log.v(TAG, json);。我查看了其他教程,它们看起来几乎相同。我无法弄清楚我的生活。任何帮助将不胜感激。

4

2 回答 2

1

void不是参考类型..

您需要使用Void这是一个引用类型

于 2013-07-04T03:49:56.180 回答
1

这个

       AsyncTask<String, void, String>

应该

        AsyncTask<String, Void, String> // V is capital

      @Override
    protected void onPostExecute(String json)
    {
        Log.v(TAG, json);//This wont print to console
    }

应该是

    @Override
    protected void onPostExecute(String json)
    {
       super.onPostExecute(json);
        Log.v(TAG, json);//This wont print to console
    }

计算结果doInBackground是 的一个参数onPostExecute

http://developer.android.com/reference/android/os/AsyncTask.html

检查 AsyncTask 泛型类型下的主题。

还要检查 4 个步骤部分下的主题。

于 2013-07-04T03:52:33.800 回答