2017-10-17 10 views
-4

에 오류가 있지만 내가 로그 캣새로운 프로그래밍입니다. 나는이 내 코드입니다 안드로이드 발리

com.example.norhanom.example2 E/AndroidRuntime에서 오류가 있었다 : 치명적인 예외를 : 주요 com.example.norhanom.example2 E/AndroidRuntime : Process : 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 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 : at android.app.ActivityThread.main (ActivityThread.java:5777) com.example.norhanom.example2 E/AndroidRuntime : java.lang.reflect.Method.invoke (네이티브 메소드) 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 :

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

이것은 PHP 없습니다. – Ivar

답변

0

당신이 당신의 축배의 오류를 볼 수있는 9, 구체적으로는 :

error.getMessage().toString() 

error 매개 변수는 항상 message 값이되지 않을 수도 있습니다, 그리고 null를 반환 할 수 있습니다.

이 문제를 해결하려면

는 토스트를 표시하기 전에 다음과 같은 체크를 넣어 :

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

여기에 두어야 했나요? – bella

+0

죄송합니다. 나는 여기에 넣었 어? public void onErrorResponse (VolleyError error) { Toast.makeText (MainActivity.this, error.getMessage(). toString(), Toast.LENGTH_LONG) .show(); } – bella