2016-08-02 6 views
-1

나는 ListView에있는 URL에서 JSON을 구문 분석하려고하지만 오류를구문 분석 JSON URL

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.vijaypremi.acmeoopsfood.util.AppController.getImageLoader()' on a null object reference 
    at com.example.vijaypremi.acmeoopsfood.adapter.CustomListAdapter.<init>(CustomListAdapter.java:26) 
    at com.example.vijaypremi.acmeoopsfood.MainActivity.onCreate(MainActivity.java:44) 
    at android.app.Activity.performCreate(Activity.java:5990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 

내 주요 활동을 보여줍니다에서입니다

요청 큐와 난의 AppController를 생성 한 이미지 로더
public class CustomListAdapter extends BaseAdapter { 

    private Activity activity; 
    private LayoutInflater inflater; 
    private List<RestaurentInfo> restaurentinfo; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

    public CustomListAdapter(Activity activity, List<RestaurentInfo> restaurentinfo) { 
     this.activity = activity; 
     this.restaurentinfo = restaurentinfo; 
    } 

    @Override 
    public int getCount() { 
     return restaurentinfo.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return restaurentinfo.get(location); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.list_row, null); 

     if (imageLoader == null) 
      imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbNail = (NetworkImageView) convertView 
       .findViewById(R.id.thumbnail); 
     TextView name = (TextView) convertView.findViewById(R.id.name); 
     TextView worth = (TextView) convertView.findViewById(R.id.worth); 
     TextView source = (TextView) convertView.findViewById(R.id.source); 
     TextView year = (TextView) convertView.findViewById(R.id.inYear); 

     // getting billionaires data for the row 
     RestaurentInfo m = restaurentinfo.get(position); 

     // thumbnail image 
     thumbNail.setImageUrl(m.getRestaurent_logo(), imageLoader); 

     // name 
     name.setText(m.getRestaurent_name()); 

     // Wealth Source 
     source.setText("Wealth Source: " + String.valueOf(m.getRestaurent_info_id())); 


     worth.setText(String.valueOf(m.getRestaurent_phone_number())); 

     // release year 
     year.setText(String.valueOf(m.getRestaurent_email())); 

     return convertView; 
    } 

} 

:

public class AppController extends Application { 

    public static final String TAG = AppController.class.getSimpleName(); 

     private RequestQueue mRequestQueue; 
     private ImageLoader mImageLoader; 

     private static AppController mInstance; 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      mInstance = this; 
     } 

     public static synchronized AppController getInstance() { 
      return mInstance; 
     } 

     public RequestQueue getRequestQueue() { 
      if (mRequestQueue == null) { 
       mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
      } 

      return mRequestQueue; 
     } 

     public ImageLoader getImageLoader() { 
      getRequestQueue(); 
      if (mImageLoader == null) { 
       mImageLoader = new ImageLoader(this.mRequestQueue, 
         new LruBitmapCache()); 
      } 
      return this.mImageLoader; 
     } 

     public <T> void addToRequestQueue(Request<T> req, String tag) { 
      // set the default tag if tag is empty 
      req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); 
      getRequestQueue().add(req); 
     } 

     public <T> void addToRequestQueue(Request<T> req) { 
      req.setTag(TAG); 
      getRequestQueue().add(req); 
     } 

     public void cancelPendingRequests(Object tag) { 
      if (mRequestQueue != null) { 
       mRequestQueue.cancelAll(tag); 
      } 
     } 
    } 
+0

이 'AppController' 클래스를 매니페스트에 추가 했습니까? (이 어댑터 = 새로운 CustomListAdapter :는이 라인의 주요 활동 오류에서 null 객체 참조 에 AppController.getImageLoader() : –

+0

예 선생님과 내가 mInstance =이는 null하여 ImageLoader에 오류가 표시 넣어 시도 RestaurentInfoList); –

+0

* mInstance = null을 넣으려고했습니다 * - 왜 그렇게 생각 했습니까? –

답변

0

Youre 요청 개체가 데이터를 검색 할 수 있지만 onResponse 덮어 쓰기에 사용하지 마십시오.

응답을 사용해야합니다. 먼저

// Creating volley request obj 
final JsonArrayRequest reqqueue = new JsonArrayRequest(url, 
     new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
       Log.d(TAG, response.toString()); 
       hidePDialog(); 
       // Parsing json 
       try { 
     /*** you wanna use the response here... ***/ 
        JSONObject jsonRootObject = new JSONObject(url); 

        //Get the instance of JSONArray that contains JSONObjects 
        JSONArray mainnode =jsonRootObject.getJSONArray("data"); 

        //Iterate the jsonArray and print the info of JSONObjects 
        for(int i=0; i < mainnode.length(); i++){ 
         JSONObject jsonObject = mainnode.getJSONObject(i); 
         JSONObject data = jsonObject.getJSONObject("restaurentInfo"); 

         RestaurentInfo restaurentinfo = new RestaurentInfo(); 
         restaurentinfo.setRestaurent_name(data.getString("re 
+0

이것은 문제가되지만 질문의 오류를 해결하지 못합니다 –

+0

네 - 맞습니다. – Hannes

+0

Sir, Response도 사용하려고했지만 위의 Log에서 언급 한 것과 같은 오류가 Line에 표시됩니다. –

0

, 당신은, 그리고

<application 
     android:name=".AppController" 

매니페스트

에 그 클래스 선언이 작동하는지 확인해야합니다. 다른 인스턴스 내부에서 인스턴스를 가져와야합니다.

public class CustomListAdapter extends BaseAdapter { 
    ....... 
    ImageLoader imageLoader; 

    public CustomListAdapter(Activity activity, List<RestaurentInfo> restaurentinfo) { 
     this.activity = activity; 
     this.restaurentinfo = restaurentinfo; 
     this.imageLoader = AppController.getInstance().getImageLoader(); 
    } 
+0

지금이 라인의 오류 : this.imageLoader = AppController.getInstance(). getImageLoader(); –

+0

AppController를 매니페스트에 추가 한 경우 오류가 발생하는 이유가 표시되지 않습니다. –