【问题标题】:android asynctask compile issuesandroid asynctask 编译问题
【发布时间】:2013-07-04 03:45:24
【问题描述】:

我正在尝试编写一个应用程序,该应用程序可以访问我朋友创建的一个网页,该网页是一个笑话,用户可以为愚蠢的事情互相给予“奖牌”。一个例子是该网站的所有者给了另一个朋友,我称他为鲍勃一枚名为“密码天才”的奖牌,上面写着“忘记了他的麻省理工学院入学密码,一般来说密码很差。”。奖牌以 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);。我查看了其他教程,它们看起来几乎相同。我无法弄清楚我的生活。任何帮助将不胜感激。

【问题讨论】:

    标签: java android multithreading compiler-errors android-asynctask


    【解决方案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 个步骤”部分下的主题。

    【讨论】:

    • onPostExecute 仍然不会打印任何内容
    • @fizyplankton 你的onCreate你的活动在哪里?
    • 没关系,是的。而不是 doInBackground 返回该测试字符串,我以为我已经打印了它。因此,当我看到 onPostExecute 打印 doInBackground 提供的内容时,我认为它是 doInBackground 打印。感谢您的帮助!
    • 我忽略了它。和进口。还有一堆其他的东西。我认为这无关紧要
    • @fizyplankton doInbackground 计算的结果是onPostExecute 的参数。请参阅 asynctask 文档
    【解决方案2】:

    void 不是引用类型..

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

    【讨论】:

    • .....这太尴尬了,哈哈。现在它可以编译了,但只有第一个 Log.v() 有效。感谢您的快速响应
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多