排球请求不会

时间:2014-08-20 10:38:28

标签: android android-volley

我使用Volley进行身份验证,但获得空指针异常。我试图 DEBUG ,但无法找到它传递null的位置。我的代码在

之下
    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String url = Const.URL_LOGIN + "?username=" + username.getText().toString() + "&password=" + password.getText().toString();


            //StringRequest requestString = new StringRequest(url, cre, errorListener)

            showProgressDialog();
            JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null, 
                    new Response.Listener<JSONObject>() {
                   @Override
                   public void onResponse(JSONObject response) {
                       VolleyLog.v("Response:%n %s", response.toString());
                       hideProgressDialog();
                   }
               }, new Response.ErrorListener() {
                   @Override
                   public void onErrorResponse(VolleyError error) {
                       VolleyLog.e("Error: ", error.getMessage());
                       hideProgressDialog();
                   }
               });
            AppController.getInstance().addToRequestQueue(req, tag_json_obj);
        }
    });

logcat的:

08-20 11:07:02.382: W/dalvikvm(2172): threadid=1: thread exiting with uncaught exception (group=0x419ae6f8) 
08-20 11:07:02.382: E/AndroidRuntime(2172): FATAL EXCEPTION: main 
08-20 11:07:02.382: E/AndroidRuntime(2172): Process: com.deliveryscience.track, PID: 2172 
08-20 11:07:02.382: E/AndroidRuntime(2172):    java.lang.NullPointerException 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    com.deliveryscience.track.MainActivity$1.onClick(MainActivity.java:79)    
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    android.view.View.performClick(View.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    android.view.View$PerformClick.run(View.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    android.os.Handler.handleCallback(Handler.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    android.os.Handler.dispatchMessage(Handler.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at android.os.Looper.loop(Looper.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    android.app.ActivityThread.main(ActivityThread.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    java.lang.reflect.Method.invokeNative(Native Method) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    java.lang.reflect.Method.invoke(Method.java:515) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)    
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
08-20 11:07:02.382: E/AndroidRuntime(2172):     at    dalvik.system.NativeStart.main(Native Method)

我编辑了它并将代码添加到我的AppController类

public class AppController extends Application {

public static final String TAG = AppController.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
  }
 }

1 个答案:

答案 0 :(得分:0)

您似乎没有在manifext.xml文件中声明应用程序类名称。

这样做:

<application
        android:name="AppController"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >