StringRequest空指针

时间:2016-05-05 08:49:23

标签: android android-volley

我已经尝试了很多东西来解决这个问题,但我无法解决这个问题 任何人都可以帮助我

     private void registerUser(final String name, final String email,
                                   final String password,final String phonenumber,final String username) {
    pDialog.setMessage("Registering ...");
    showDialog();
    StringRequest stringRequest = new StringRequest(Method.POST, "http://192.168.2.103/Bspark/addPengguna.php", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            pDialog.dismiss();
            Toast.makeText(Register.this, "Register the Data", Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            pDialog.dismiss();
            Toast.makeText(Register.this, "Error Data Cannot be Registered", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("username", username);
            params.put("password",password);
            params.put("email",email);
            params.put("phone", phonenumber);

            return params;
        }
    };
    Log.d(TAG, "registerUser: "+stringRequest);
    AppController.getInstance().addToReqQueue(stringRequest);


}

这是我的php文件

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){

    include("db_connect.php");

//Getting values
    $name = $_POST['name'];
    $username = $_POST['username'];
    $password = md5($_POST['password']);
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $response = array();    
    //Creating an sql query
    $result = "INSERT INTO pengguna (nama, username, password, email, phone) VALUES ('$name','$username','$password','$email','$phone')";
    //Executing query to database
    if($result>0){
       $response["success"] = 1;
     }    
    else{
       $response["success"] = 0;
     }
 // echoing JSON response
    echo json_encode($response);
    //Closing the database
}

&GT;

我的错误是

  

05-05 15:39:56.414 6883-6883 /? D /寄存器:registerUser:[] http://192.168.2.103/Bspark/addPengguna.php 0x85ca2559 NORMAL null

     

05-05 15:39:56.416 6883-6883 /? E / AndroidRuntime:致命异常:主要                                                    处理:com.example.dennis.bspark,PID:6883                                                    java.lang.NullPointerException:尝试调用虚方法&void; com.andample.volley.Request)在null对象引用上                                                        在com.example.dennis.bspark.Register.registerUser(Register.java:89)                                                        在com.example.dennis.bspark.Register.onClick(Register.java:56)                                                        在android.view.View.performClick(View.java:4789)                                                        在android.view.View $ PerformClick.run(View.java:19881)                                                        在android.os.Handler.handleCallback(Handler.java:739)                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                        在android.os.Looper.loop(Looper.java:135)                                                        在android.app.ActivityThread.main(ActivityThread.java:5289)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我已经尝试log.d但是没有nullpointer 请帮助我谢谢

我的AppController类

private RequestQueue mRequestQueue;
private static AppController mInstance;

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

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

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

    return mRequestQueue;
}

public <T> void addToReqQueue(Request<T> req, String tag) {

    getReqQueue().add(req);
}

public <T> void addToReqQueue(Request<T> req) {

    getReqQueue().add(req);
}

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

1 个答案:

答案 0 :(得分:0)

您的AppController已连线,上下文应从其构造函数中获取,如下所示:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap>
                    cache = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

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

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
}