Sto testando la mia app sia su emulatore virtuale ed è Ok ma sul telefono parte ma quando devo fare una ricerca sol db mi da errore:
10-22 18:55:07.300 3205-3205/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paolo.myapplication, PID: 3205
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.example.paolo.myapplication.MainActivity$2.onErrorResponse(MainActivity.java:81)
at com.android.volley.Request.deliverError(Request.java:598)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Questo è il MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText eTNbusta,eTCosto, eTSearch;
private String str_nBusta,str_costo;
private Button btnSearch,btnSalva;
private ProgressDialog pdLoading ,pdUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTSearch = (EditText)findViewById(R.id.eTSearch);
eTNbusta = (EditText)findViewById(R.id.eTCodice);
eTCosto = (EditText)findViewById(R.id.eTCosto);
btnSearch = (Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(this);
btnSalva = (Button)findViewById(R.id.btnSalva);
btnSalva.setOnClickListener(this);
}
private void getData()
{
String Nbusta = eTSearch.getText().toString().trim();
if (Nbusta.equals(""))
{
Toast.makeText(this,"Inserire Numero Busta",Toast.LENGTH_LONG).show();
return;
}
pdLoading = ProgressDialog.show(this,"Attendere","Caricamento",false,false);
String url = Config.DATA_URLCASA + eTSearch.getText().toString().trim();
Log.d("Controllo_URL.....", url);
final StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response)
{
pdLoading.dismiss();
showJSON(response);
Log.d("MYAPP", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
private void showJSON(String response)
{
String nBusta ="";
String costo = "";
try {
response.replaceFirst("\\r\\n1","");
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("tabripa");
JSONObject collegaData = result.getJSONObject(0);
nBusta = collegaData.getString(Config.KEY_NBUSTA);
costo = collegaData.getString(Config.KEY_COSTO);
} catch (JSONException e) {
e.printStackTrace();
}
eTNbusta.setText(nBusta);
eTCosto.setText(costo);
}
public void update()
{
String nBusta = eTNbusta.getText().toString();
String costo = eTCosto.getText().toString();
Map<String,String> params = new HashMap<>();
params.put("Nbusta", nBusta);
params.put("costo",costo);
Log.d("update.......", String.valueOf(params));
CustomRequest update_request = new CustomRequest(Request.Method.POST, Config.DATA_UPDATE,
params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("UpdateRisposta.......", String.valueOf(response));
try{
int success = response.getInt("success");
if (success == 1) {
Toast.makeText(getApplicationContext(),
"Aggiornamento Riuscito",
Toast.LENGTH_SHORT).show();
// redirect to readdata
// MoveToReadData();
} else {
Toast.makeText(getApplicationContext(),
"Aggiornamento fallito", Toast.LENGTH_SHORT)
.show();
}
}
catch (Exception e){
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(this).addToRequestQueue(update_request);
}
@Override
public void onClick(View v)
{
Button b = (Button) v;
switch(b.getId()) {
case R.id.btnSearch:
getData();
break;
case R.id.btnSalva:
update();
break;
}
}
}