使用google volley发布数据时出错:NullPointerException:尝试调用虚方法'int java.lang.String.hashCode()'

时间:2016-09-27 10:08:27

标签: android-volley getjson

我正在尝试使用android google排球使用localhost发布数据,我得到以下异常....当使用html页面测试时,php脚本工作正常。但是当我点击android注册上的下一个按钮时页面它崩溃.Below是我的RegisterDialog.class的我的java代码,MySingleton.class是控制器,php script.pls帮助我。

09-27 15:10:57.037 13859-13859/com.example.admin.loginandregister E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.admin.loginandregister, PID: 13859
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
                                                                                    at com.android.volley.Request.<init>(Request.java:136)
                                                                                    at com.android.volley.toolbox.StringRequest.<init>(StringRequest.java:43)
                                                                                    at com.example.admin.loginandregister.RegisterDialog$4$3.<init>(RegisterDialog.java:0)
                                                                                    at com.example.admin.loginandregister.RegisterDialog$4.onClick(RegisterDialog.java:136)
                                                                                    at android.view.View.performClick(View.java:4793)
                                                                                    at android.view.View$PerformClick.run(View.java:19902)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5357)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:952)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)

RegisterDialog.java

package com.example.admin.loginandregister;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

 import com.andexert.library.RippleView;
 import com.android.volley.AuthFailureError;
 import com.android.volley.Request;
 import com.android.volley.Response;
 import com.android.volley.VolleyError;
 import com.android.volley.toolbox.StringRequest;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


 public class RegisterDialog extends DialogFragment {
Button nextbt,closebt;
EditText Name,Email,Username,Password,ConPassword;
String name,email,username,password,conpass;
AlertDialog.Builder builder;
ScrollView sv;
RelativeLayout relativeLayout;
final Handler handler = new Handler();
String reg_url="http;//192.168.1.21/register/register.php";


public static RegisterDialog newInstance(String param1, String param2) {
    RegisterDialog fragment = new RegisterDialog();
    Bundle args = new Bundle();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE,0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.registerfrag, container, false);
    //relativeLayout=(RelativeLayout)view.findViewById(R.id.relLayout);
    nextbt=(Button)view.findViewById(R.id.go_bt);
    Name=(EditText)view.findViewById(R.id.reg_Name);
    Email=(EditText)view.findViewById(R.id.reg_Email);
    Username=(EditText)view.findViewById(R.id.reg_uName);
    Password=(EditText)view.findViewById(R.id.reg_PassWord);
    ConPassword=(EditText)view.findViewById(R.id.reg_confirmPassWord);

    closebt=(Button)view.findViewById(R.id.close_bt);
    builder=new AlertDialog.Builder(this.getActivity());
    closebt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    sv = (ScrollView) view.findViewById(R.id.sv);
    sv.setOnTouchListener(new View.OnTouchListener() {
                                          @Override
                                          public boolean onTouch(View v, MotionEvent event) {
                                              sv.setBackgroundResource(R.drawable.ripple);
                                              return false;
                                          }
                                      });



       handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 200ms
            //if(setRipple())
            //{
            Log.i("Executing:","Ripple Effect");
            setRipple();
            //sv.setBackgroundDrawable( getResources().getDrawable(R.color.colorDarkMagenta) );
            //}
        }
    }, 50);
    Log.i("Executing:","Ripple Effect");
    sv.setBackgroundResource(R.color.colorLightGrey);

    //setRipple();



    nextbt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //DoRegistration();
            name = Name.getText().toString();
            email = Email.getText().toString();
            username = Username.getText().toString();
            password = Password.getText().toString();
            conpass = ConPassword.getText().toString();
            if (name.equals("") || email.equals("") || username.equals("") || password.equals("") || conpass.equals("")) {
                builder.setTitle("Something went wrong");
                builder.setMessage("Please fill all the fields");
                displayAlert("input error");
            }

            if (!password.equals(conpass)) {
                builder.setTitle("Something went wrong");
                builder.setMessage("Your Passwords do not match");
                displayAlert("input error");
            } else {
                StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            JSONObject jsonObject = jsonArray.getJSONObject(0);
                            String code = jsonObject.getString("code");
                            jsonObject.getString("message");
                            builder.setTitle("Server Response");
                            displayAlert(code);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("name", name);
                        params.put("username", name);
                        params.put("email", name);
                        params.put("password", name);

                        return params;

                    }
                };
                MySingleton.getmInstance(RegisterDialog.this.getActivity()).addToRequestque(stringRequest);
                final Animation myAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.bounce);

                BounceInterpolator interpolator = new BounceInterpolator(.1, 5);
                myAnim.setInterpolator(interpolator);
                nextbt.setText(getResources().getString(R.string.tick_mark));
                nextbt.startAnimation(myAnim);
            /*Intent i=new Intent(getActivity(),RegDone.class);
            startActivity(i);*/
            }
        }});

    return view;
}

 public boolean setRipple()
    {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 5000s
            //if(setRipple())
            //{
                Log.i("Executing:","Change Background Color");
                sv.setBackgroundDrawable( getResources().getDrawable(R.color.colorDarkMagenta) );
            //}
        }
    }, 300);
    Log.i("Executing:","Ripple Effect");
    sv.setBackgroundResource(R.drawable.ripple);
    return true;
}

public void displayAlert(final String code)
{

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(code.equals("input error"))
        {
            Password.setText("");
            ConPassword.setText("");
        }
        else if(code.equals("reg successful"))
        {
            dismiss();
        }
        else
        {
            Name.setText("");
            Username.setText("");
            Email.setText("");
            Password.setText("");
            ConPassword.setText("");
        }

    }
});
    AlertDialog alertDialog=builder.create();
    alertDialog.show();
  }
  }

MySingleton.class

 package com.example.admin.loginandregister;

 import android.content.Context;

 import com.android.volley.Request;
 import com.android.volley.RequestQueue;
 import com.android.volley.toolbox.Volley;

 /**
 * Created by ADMIN on 27-09-2016.
 */
 public class MySingleton
{
private static MySingleton mInstance;
private RequestQueue requestQueue;
private Context mCtx;
private MySingleton(Context context)
{
    mCtx=context;
    requestQueue=getRequestQueue();
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());

                              }
    return requestQueue;

                                       }
public static synchronized MySingleton getmInstance(Context context)
{
  if (mInstance==null)
  {
      mInstance=new MySingleton((context));
  }
    return mInstance;
}
public<T> void addToRequestque(Request<T> request)
    {
    requestQueue.add(request);
    }

   }

register.php

<?php
//youtube video

include("init.php");

/*$sql="select '*' from user_info where email like '".$email."';";

$result=mysqli_query($con,$sql);
$response=array();
if(mysqli_num_rows($result)>0)
{
    $code="reg failed";
    $message="User already exist...";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode($response);
}
else*/

$sql = "INSERT INTO `user_info`(`name`, `email`, `username`,`password`)
VALUES ('$_POST[name]', '$_POST[email]',     '$_POST[username]','$_POST[password]')";
$result=mysqli_query($con,$sql);
$response=array();
$resp=array();
$code="reg successful";
    $message="Thank you for registering with us.Now you can login";
    array_push($resp,array(  "name"=>$_POST['name'],"email"=>$_POST['email'],"username"=>$_POST['username'],"password"=>$_POST['password']));
    array_push($response,array( "code"=>$code,"message"=>$message,"details"=>$resp));
    echo json_encode($response);
    $jsonresp=json_encode($response);
     $jsonresp=json_decode($jsonresp,true);

    mysqli_close($con);

  ?>

0 个答案:

没有答案