2017-10-17 28 views
-4

Ceci est mon code, mais j'avais une erreur dans logcatJe suis nouveau en programmation. j'ai une erreur volée Android

com.example.norhanom.example2 E/AndroidRuntime: FATAL EXCEPTION: principale com.example.norhanom.example2 E/AndroidRuntime: Processus: com.example.norhanom.example2, PID: 26334 com.example.norhanom.example2 E/AndroidRuntime: java.lang.NullPointerException: Tentative d'invocation de la méthode virtuelle 'java.lang.String java.lang.String. toString() 'sur une référence d'objet null com.example.norhanom.example2 E/AndroidRuntime: at com.example.norhanom.example2.MainActivity $ 2.onErrorResponse (MainActivity.java:68) com.example.norhanom.exemple2 E/AndroidRuntime: at com.android.volley.Request.deliverError (Request.java:564) com.example.norhanom.example2 E/AndroidRuntime: à 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: à l'adresse android.os.Handler.dispatchMessage (Handler.java:104) com.example.norhanom.example2 E/AndroidRuntime: at android. os.Looper.loop (Looper.java:207) com.example.norhanom.example2 E/AndroidRuntime: à l'adresse android.app.ActivityThread.main (ActivityThread.java:5777) com.example.norhanom.example2 E/AndroidRuntime : à java.lang.reflect.Method.invoke (méthode native) com.example.norhanom.example2 E/AndroidRuntime: à com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:789) com.example.norhanom.example2 E/AndroidRuntime: à com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679) com.example.norhanom.exemple2 I/Processus: Signal d'envoi. 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] 
+3

Ce n'est pas PHP. – Ivar

Répondre

0

Comme vous pouvez le voir l'erreur dans votre pain grillé, plus précisément à:

error.getMessage().toString() 

Le paramètre error peut pas toujours avoir une valeur message, et pourrait renvoyer un null.

Pour résoudre ce problème, mettez le contrôle suivant avant de montrer le toast:

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

je devais mettre à ici? – bella

+0

désolé. j'avais mis ici? public void onErrorResponse (erreur VolleyError) { Toast.makeText (MainActivity.this, error.getMessage(). toString(), Toast.LENGTH_LONG) .show(); } – bella