我是第一次使用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)
在我的 MainActivity 中:
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();
}
}
});
}
我直接从他们的示例中获取了这段代码,但我看不出我有什么问题。
谢谢你的帮助。