简体   繁体   English

android asynctask编译问题

[英]android asynctask compile issues

I'm trying to write an app that accesses a webpage my friend created as a joke where users can give "medals" to one another for stupid things. 我正在尝试编写一个应用程序来访问我的朋友创建的网页作为一个笑话,用户可以为愚蠢的事情互相给予“奖牌”。 an example is the owner of the site gave another friend whom i shall call bob a medal called "password genius" with the description "Forgot his MIT admissions password and is just bad at passwords in general.". 一个例子是该网站的所有者给了另一个朋友,我将其称为“密码天才”,其描述为“忘记了他的麻省理工学院招生密码,而且一般情况下密码不好”。 the medals are retrieved in JSON format, and the above example would be 奖牌以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....]
. I want to retrieve these using an AsyncTask in my app, but i get the weirdest compiler errors. 我想在我的应用程序中使用AsyncTask检索这些,但我得到了最奇怪的编译器错误。 my code is, in a nutshell, 简而言之,我的代码是

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
        }
    }
}

if I try to do that, I get 如果我试着这样做,我明白了

[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

however, if I replace 但是,如果我更换

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

with

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

it will compile, but only the Log.v(TAG, "this will print to console"); 它会编译,但只有Log.v(TAG,“这将打印到控制台”); actually prints, not the Log.v(TAG, json) ;. 实际打印,而不是Log.v(TAG, json) I have looked up other tutorials, and they look pretty much identical. 我已经查找了其他教程,它们看起来非常相似。 i cant figure out for the life of me. 我无法弄清楚我的生活。 any help would be greatly appreciated. 任何帮助将不胜感激。

void is not a reference type.. void不是引用类型..

You need to use Void which is a reference type 您需要使用Void作为参考类型

This 这个

       AsyncTask<String, void, String>

should be 应该

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

Also

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

shoudl be shoudl是

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

The result of doInBackground computation is a paramter to onPostExecute . 结果doInBackground计算是一个参数设置为onPostExecute

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

Check the topic under AsyncTask generic types. 检查AsyncTask泛型类型下的主题。

Also check the topic under the section The 4 steps. 另请查看“4个步骤”部分下的主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM