protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expenses__add);
amount = (EditText) findViewById(R.id.et_amount);
date1 = (EditText) findViewById(R.id.date);
button = (Button) findViewById(R.id.btn_add);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
amount1 = amount.getText().toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = date1.getText().toString();
try {
Date date = formatter.parse(dateInString);
myText=formatter.format(date);
}
catch (ParseException e) {
e.printStackTrace();
}
Toast.makeText(Expenses_Add.this,myText,Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(Expenses_Add.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("date",String.valueOf( myText));
params.put("email", "bdshah437@gmail.com");
params.put("amount", amount1);
return params;
}
};
requestQueue.add(stringRequest);
} protected void onStart() {
super.onStart();
EditText editText = (EditText) findViewById(R.id.date);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
DateDialog dateDialog = new DateDialog(view);
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
dateDialog.show(ft, "DatePicker");
}
}
});
}
}
有错误
09-11 04:20:48.006 16973-17061 / com.example.bhoomi.household E / Volley: [42344] NetworkDispatcher.run:未处理的异常 显示java.lang.NullPointerException 显示java.lang.NullPointerException 在libcore.net.UriCodec.encode(UriCodec.java:132) 在java.net.URLEncoder.encode(URLEncoder.java:57) 在com.android.volley.Request.encodeParameters(Request.java:450) 在com.android.volley.Request.getBody(Request.java:436) 在 com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260) 在 com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234) 在 com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107) 在 com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96) 在 com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
答案 0 :(得分:0)
创建新的MySingleton类
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;
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
要求点击按钮
StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("---response--", "" + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error---", "" + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("date",String.valueOf( myText));
params.put("email", "bdshah437@gmail.com");
params.put("amount", amount1);
return params;
}
};
MySingleton.getInstance(Expenses_Add.this).addToRequestQueue(stringRequest);