English 中文(简体)
使用 Json Asynch Task 的 AQuery 为 JSON Asynch 设置的 NPE 查询器
原标题:NPE using AQuery for JSON AsynchTask

我第一次使用"http://code.google.com/p/android-query/" rel="nofollow">Android-Query ,我明白它(照顾你无序的任务?)和简化/减少代码的写法。如果我对无序的任务错了,请纠正我。

此外,如果我错了,请帮我写下下面的方法,这样它就会在另一条线上;无论我怎么做,我都不能让它工作。

我收到以下NPE:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andaero.app/com.andaero.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)at dalvik.system.NativeStart.main(Native Method)
--->Caused by: java.lang.NullPointerException
at com.andaero.app.utili.AsyncJSON.async_list_array(AsyncJSON.java:24)
at com.andaero.app.MainActivity.onCreate(MainActivity.java:84)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

在我的主要活动范围内:

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
 ...
 AQuery aq;
 String json = null;

public class MainActivity extends Activity implements AnimationLayout.Listener
{
 @Override
 public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ...
        AsyncJSON.async_list_array();
    }
}

我的AsyncJSON班:

 public class AsyncJSON
 {
    static AQuery aq = null;
    static JSONArray jArray;
    String json = null;

public static void async_list_array()
{

String url = "http://192.168.1.34/Andaero/php/regulatory_list.php";
aq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>()
{

    @Override
    public void callback(String url, JSONArray json, AjaxStatus status)
    {

    if (json != null)
    {

    // successful ajax call, show status code, json content
    //jsonListCallback(json);
    Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();

    } else
    {

    // ajax error, show error code
    Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
    }
    }
});
}

我直接从他们的例子中取出了这个代码 但我看不出我有什么不对劲

麻烦你帮忙

最佳回答

看起来你从未初始化过aq ,所以是无效的。

问题回答

主要活动

aq=new AQuery(this);
AsyncJSON.async_list_array(aq);

在 AsyncJson 类

static AQuery maq;
public static void async_list_array(AQuery aq)
{
    maq=aq;
    String url="http://192.168.1.34/Andaero/php/regulatory_list.php";

    maq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>(){
        @Override
        public void callback(String url,JSONArray json,AjaxStatus status)
        {
            if(json!=null)
            {
                Toast.makeText(maq.getContext(), status.getCode()+":"+json.toString(), Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(maq.getContext(), "Error:"+status.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });

}

在您的宣言文件中添加互联网权限 。

也检查网络连接 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签