在Android应用程序中获取数据时遇到麻烦

时间:2016-01-06 11:12:11

标签: android json exception

嘿开发人员我在Android应用程序中获取数据时遇到错误。以下是我的错误日志:

01-06 16:38:24.535: E/Volley(13308): [7815] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
01-06 16:38:24.535: E/Volley(13308): java.lang.NullPointerException
01-06 16:38:24.535: E/Volley(13308):    at libcore.net.UriCodec.encode(UriCodec.java:132)
01-06 16:38:24.535: E/Volley(13308):    at java.net.URLEncoder.encode(URLEncoder.java:57)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.Request.encodeParameters(Request.java:449)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.Request.getBody(Request.java:435)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
01-06 16:38:24.535: E/Volley(13308):    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
01-06 16:38:24.576: E/Error.Response(13308): com.android.volley.VolleyError: java.lang.NullPointerException
这是我的代码//// 请帮我找到正确的问题。我不知道logcat中有什么错误。

public class Edit_Address extends Activity implements OnClickListener,
    TextWatcher {

String[] con = { "India" };
String[] st = { "--- Please Select State ---", "Andhra Pradesh",

        "Orissa",
        "Telangana", "Tripura", "Uttar Pradesh", "Uttarakhand",
        "West Bengal" };
Spinner state, country;
String cartprice = "", cartimage = "", cartdesc = "", cartmodelno = "",
        cartqty = "", cartproductid = "", cartintoprice = "";
EditText email, mobile, name, add1, add2, city, postcode;
TextView termstitle;
Button save;
String pname, pemail, pmobile, padd1, padd2, pcity, ppostcode,
        shipstatus = "no", pcountry, pstate, status, orderid, subtotal,
        shippingcharge, fpName, gpName, fpEmail, gpEmail;
String spname, spemail, spmobile, spadd1, spadd2, spcity, sppostcode,
        sshipstatus = "no", spcountry, spstate, flag, fspName, gspName,
        fspEmail, gspEmail;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_address);
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#ffffff")));

    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setCustomView(R.layout.abs_layout);

    View v = getActionBar().getCustomView();
    TextView titleTxtView = (TextView) v.findViewById(R.id.mytext);

    titleTxtView.setText("Edit Address");
    email = (EditText) findViewById(R.id.emailbill);
    mobile = (EditText) findViewById(R.id.mobilebill);
    name = (EditText) findViewById(R.id.namebill);
    add1 = (EditText) findViewById(R.id.add1bill);
    add2 = (EditText) findViewById(R.id.add2bill);
    city = (EditText) findViewById(R.id.citybill);
    save = (Button) findViewById(R.id.save);
    postcode = (EditText) findViewById(R.id.postbill);

    save.setOnClickListener(this);
    email.addTextChangedListener(this);
    mobile.addTextChangedListener(this);
    name.addTextChangedListener(this);
    add1.addTextChangedListener(this);
    postcode.addTextChangedListener(this);
    city.addTextChangedListener(this);

    state = (Spinner) findViewById(R.id.statebill);
    country = (Spinner) findViewById(R.id.countrybill);

    ArrayAdapter aa = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, st);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Setting the ArrayAdapter data on the Spinner
    state.setAdapter(aa); ////error shows in this line

    ArrayAdapter cc = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, con);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    country.setAdapter(cc);

    Intent i = getIntent();
    pname = i.getStringExtra("name");
    // fpName = i.getStringExtra("name");
    // gpName = i.getStringExtra("name");
    // fpEmail = i.getStringExtra("email");
    pemail = i.getStringExtra("email");
    // gpEmail = i.getStringExtra("email");
    pmobile = i.getStringExtra("mobile");
    padd1 = i.getStringExtra("add1");
    padd2 = i.getStringExtra("add2");
    pcity = i.getStringExtra("city");
    pcountry = i.getStringExtra("country");
    pstate = i.getStringExtra("state");
    ppostcode = i.getStringExtra("pincode");

    email.setText(pemail);
    // email.setText(fpEmail);
    // email.setText(gpEmail);
    // name.setText(fpName);
    // name.setText(gpName);
    name.setText(pname);
    mobile.setText(pmobile);
    add1.setText(padd1);
    add2.setText(padd2);
    postcode.setText(ppostcode);
    city.setText(pcity);
    if (!pstate.equals(null)) {
        int spinnerPostion = aa.getPosition(pstate);
        state.setSelection(spinnerPostion);
        spinnerPostion = 0;
    }
    if (!pcountry.equals(null)) {
        int spinnerPostion = cc.getPosition(pcountry);
        country.setSelection(spinnerPostion);
        spinnerPostion = 0;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    try {
        pname = name.getText().toString();
        pemail = email.getText().toString();
        // fpName = name.getText().toString();
        // fpEmail = email.getText().toString();
        // gpName = name.getText().toString();
        // gpEmail = email.getText().toString();
        padd1 = add1.getText().toString();
        padd2 = add2.getText().toString();
        ppostcode = postcode.getText().toString();
        pcity = city.getText().toString();
        pstate = state.getSelectedItem().toString();
        pcountry = country.getSelectedItem().toString();
        pmobile = mobile.getText().toString();

        if (v == save) {

            if (email.getText().toString().trim().length() == 0) {
                email.setError("Please eneter email Id.");
                email.requestFocus();

            } else if (!android.util.Patterns.EMAIL_ADDRESS.matcher(
                    email.getText().toString()).matches()) {
                email.setError("Email address should contains a valid maild_id format !!");
                email.requestFocus();
            }

            else if (mobile.getText().toString().trim().length() == 0) {
                mobile.setError("Please eneter valid number");
                mobile.requestFocus();

            }

            else if (name.getText().toString().trim().length() == 0) {
                name.setError("Please eneter name");
                name.requestFocus();

            }

            else if (add1.getText().toString().trim().length() == 0) {
                add1.setError("Please eneter address");
                add1.requestFocus();

            }

            else if (city.getText().toString().trim().length() == 0) {
                city.setError("Please eneter city");
                city.requestFocus();

            }

            else if (postcode.getText().toString().trim().length() == 0) {
                postcode.setError("Please eneter pin code");
                postcode.requestFocus();

            }

            else {

                fetch_data();
            }
        }
    } catch (NullPointerException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

ProgressDialog pDialog;

private void fetch_data() {
    // TODO Auto-generated method stub
    // bar.setVisibility(View.VISIBLE);
    pDialog = new ProgressDialog(Edit_Address.this);
    pDialog.setMessage("Please Wait...");
    pDialog.setCancelable(false);
    pDialog.show();
    StringRequest req = new StringRequest(Request.Method.POST,
            Config.YOUR_API_URL + "updateuserinformation.aspx",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // bar.setVisibility(View.GONE);
                    // response
                    pDialog.dismiss();
                    Log.e("Response", response);
                    try {
                        JSONObject obj = new JSONObject(response);
                        int success = obj.getInt("response");
                        if (success == 1) {

                            showconfirm();
                        } else if (success == 0) {
                            Toast.makeText(getApplicationContext(),
                                    "error occured", Toast.LENGTH_LONG)
                                    .show();
                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Log.e("exception", "ex" + e.toString());
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // bar.setVisibility(View.GONE);
                    pDialog.dismiss();
                    showerrordialog();
                    // error
                    // Log.d("Error.Response", response);
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            // String pid=String.valueOf(produtid);
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", pname);
            params.put("email", pemail);
            // params.put("name", fpName);
            // params.put("email", fpEmail);
            // params.put("name", gpName);
            // params.put("email", gpEmail);
            params.put("Address1", padd1);
            params.put("Address2", padd2);
            params.put("City", pcity);
            params.put("Zip", ppostcode);
            params.put("State", pstate);
            params.put("Country", pcountry);
            params.put("ContactNo", pmobile);

            return params;
        }
    };
    AppController.getInstance().addToRequestQueue(req);

}

protected void showerrordialog() {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(
            "It's taking Longer than usuall.Do You want to wait?")
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    fetch_data();
                }
            });

    // Creating dialog box
    AlertDialog alert = builder.create();
    // Setting the title manually
    alert.setTitle(" Issue !");
    alert.show();
}

protected void showconfirm() {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your detail is sucessfully added.")
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Intent i = new Intent(getApplicationContext(),
                            Show_Address.class);
                    startActivity(i);
                }
            });

    // Creating dialog box
    AlertDialog alert = builder.create();
    // Setting the title manually
    alert.setTitle(" Issue !");
    alert.show();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

    if (name.getText().length() > 0) {
        name.setError(null);
        name.setSelection(name.length());
    }
    if (email.getText().length() > 0) {
        email.setError(null);
        email.setSelection(email.length());
    }

    if (mobile.getText().length() > 0) {
        mobile.setError(null);
        mobile.setSelection(mobile.length());
    }
    if (add1.getText().length() > 0) {
        add1.setError(null);
        add1.setSelection(add1.length());
    }

    if (city.getText().length() > 0) {
        city.setError(null);
        city.setSelection(city.length());
    }
    if (postcode.getText().length() > 0) {
        postcode.setError(null);
        postcode.setSelection(postcode.length());
    }

}

}

感谢 提前

0 个答案:

没有答案