我是编程新手。我在android volley

时间:2017-10-17 13:33:06

标签: java android android-studio android-volley logcat

这是我的代码,但我在logcat中有错误

com.example.norhanom.example2 E / AndroidRuntime:FATAL EXCEPTION:main com.example.norhanom.example2 E / AndroidRuntime:进程:com.example.norhanom.example2,PID:26334 com.example.norhanom.example2 E / AndroidRuntime:java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.String.toString()'在null对象引用上 com.example.norhanom.example2 E / AndroidRuntime:at com.example.norhanom.example2.MainActivity $ 2.onErrorResponse(MainActivity.java:68) com.example.norhanom.example2 E / AndroidRuntime:at com.android.volley.Request.deliverError(Request.java:564) com.example.norhanom.example2 E / AndroidRuntime:at com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:101) com.example.norhanom.example2 E / AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:815) com.example.norhanom.example2 E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:104) com.example.norhanom.example2 E / AndroidRuntime:在android.os.Looper.loop(Looper.java:207) com.example.norhanom.example2 E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5777) com.example.norhanom.example2 E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method) com.example.norhanom.example2 E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) com.example.norhanom.example2 E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) com.example.norhanom.example2 I / Process:发送信号。 PID:26334 SIG:9

 MainActivity
package com.example.norhanom.example2;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

private EditText editTextBarcodeNumber;
private Button buttonGet;
private TextView textViewResult;

private ProgressDialog loading;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editTextBarcodeNumber = (EditText) findViewById(R.id.editTextbc);
    buttonGet = (Button) findViewById(R.id.button);
    textViewResult = (TextView) findViewById(R.id.textViewResult);

    buttonGet.setOnClickListener(this);

}

private void getData() {
    String bc = editTextBarcodeNumber.getText().toString().trim();
    if (bc.equals("")) {
        Toast.makeText(this, "Please enter the barcode number", 
     Toast.LENGTH_LONG).show();
        return;
    }
    loading = ProgressDialog.show(this,"Please 
     wait...","Fetching...",false,false);

    String url = 
 Config.DATA_URL+editTextBarcodeNumber.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(url, new 
 Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
    String name="";
    String StatusProduct="";
    String ExpiredDate = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject halal_data = result.getJSONObject(0);
        name = halal_data.getString(Config.KEY_NAME);
        StatusProduct = halal_data.getString(Config.KEY_STATUSPRODUCT);
        ExpiredDate = halal_data.getString(Config.KEY_EXPIREDDATE);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("Name:\t" + name + "\nStatus of Product:\t" + 
 StatusProduct + "\nExpired Date:\t"+ ExpiredDate);
}

@Override
public void onClick(View v) {
    getData();
}
}


Config.java
 package com.example.norhanom.example2;


 public class Config {
public static final String DATA_URL = 
"http://192.168.1.7/android/getData.php?BarcodeNumber=";
public static final String KEY_NAME = "Name";
public static final String KEY_STATUSPRODUCT = "StatusProduct";
public static final String KEY_EXPIREDDATE = "ExpiredDate";
public static final String JSON_ARRAY = "result";
}


activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GET"
    android:id="@+id/button"
    android:layout_below="@+id/editTextbc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editTextbc"
    android:text="enter the barcode number"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp" />

<TextView
    android:id="@+id/textViewResult"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="55dp" />

   [enter image description here][1]

1 个答案:

答案 0 :(得分:0)

您可以在吐司中看到错误,特别是在:

error.getMessage().toString()

error参数的值可能并不总是message,并且可能会返回null

要解决此问题,请在显示toast之前进行以下检查:

If (error.getMessage() != null) { // show toast }