2016-08-04 53 views
2

我想在滑動菜單片段中顯示json數據,但是當我輸入片段應用程序意外關閉時,我有34個片段,我需要爲每個片段顯示不同的Json數據。感謝您的幫助,我希望您能理解這個問題。如何在滑動菜單片段中顯示json數據?

LOGCAT

DahiliyeFragment

public class DahiliyeFragment extends Fragment { 


    public DahiliyeFragment() { 
    // Required empty public constructor 
    } 
    private ListView lvPersonel; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 


     new JSONTask().execute("https://api.myjson.com/bins/4hopr"); 

     DisplayImageOptions defaultOptions = new    DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build(); 
     ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(  getActivity()).defaultDisplayImageOptions(defaultOptions) 
     .build(); 
     ImageLoader.getInstance().init(config); // Do it on Application start 

     lvPersonel = (ListView) getView().findViewById(R.id.lvPersonel); 


     return inflater.inflate(R.layout.fragment_dahiliye, container, false); 
    } 



    public class JSONTask extends AsyncTask<String,String,List<PersonelModel>> 
    { 

    @Override 
    protected List<PersonelModel> doInBackground(String... params) { 


     HttpURLConnection connection = null; 
     BufferedReader reader=null; 
     InputStream stream; 
     StringBuffer buffer; 
    try { 
     URL url = new URL(params[0]); //bağlantı 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 

     stream = connection.getInputStream(); 
     reader = new BufferedReader(new InputStreamReader(stream)); 
     buffer = new StringBuffer(); 
     String line = ""; 
     while ((line = reader.readLine()) != null) 
     { 
      buffer.append(line); 

     } 

     String finalJson = buffer.toString(); 
     JSONObject parentObject = new JSONObject(finalJson); 

     JSONArray parentArray = parentObject.getJSONArray("personeller"); 

     List<PersonelModel> personelModelList = new ArrayList<>(); 

     for(int i=0; i<parentArray.length(); i++) 

     { 
      JSONObject finalobject = parentArray.getJSONObject(i); 


      PersonelModel personelModel = new PersonelModel(); 
      personelModel.setUnvan(finalobject.getString("unvan")); 
      personelModel.setAd(finalobject.getString("ad")); 
      personelModel.setSoyad(finalobject.getString("soyad")); 
      personelModel.setOda(finalobject.getString("oda")); 
      personelModel.setResim(finalobject.getString("resim")); 




      personelModelList.add(personelModel); 



      } 


      return personelModelList; 


      } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } catch (JSONException e) { 
      e.printStackTrace(); 
      } finally { 
      if(connection != null) 
      {connection.disconnect();} 
      try { 
      if(reader != null){ 
       reader.close();} 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
      } 

      return null; 
      } 



      @Override 
      protected void onPostExecute(List<PersonelModel> result) { 
       super.onPostExecute(result); 

       PersonelAdapter adapter = new       PersonelAdapter(getActivity(),R.layout.row, result); 
       lvPersonel.setAdapter(adapter); 

      } 
      } 



      public class PersonelAdapter extends ArrayAdapter{ 

       private List<PersonelModel> personelModelList; 
       private int resource; 
       private LayoutInflater inflater; 

      public PersonelAdapter(Context context, int resource, List<PersonelModel> objects) { 
       super(context, resource, objects); 
       personelModelList = objects; 
       this.resource = resource; 
       inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      } 

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

       ViewHolder holder = null; 
       if(convertView == null) 
       { 
       holder = new ViewHolder(); 
       convertView = inflater.inflate(resource, null); 
       holder.ivResim =  (ImageView)convertView.findViewById(R.id.ivResim); 
       holder.tvAd = (TextView)convertView.findViewById(R.id.tvAd); 
       holder.tvSoyad = (TextView)convertView.findViewById(R.id.tvSoyad); 
       holder.tvOda = (TextView)convertView.findViewById(R.id.tvOda); 
       holder.tvUnvan = (TextView)convertView.findViewById(R.id.tvUnvan); 

       convertView.setTag(holder); 
      } 
      else 
      { 
       holder = (ViewHolder) convertView.getTag(); 
      } 




       final ProgressBar progressBar =  (ProgressBar)convertView.findViewById(R.id.progressBar); 
         ImageLoader.getInstance().displayImage("http://i.hizliresim.com/go7bpQ.jpg", holder.ivResim, new ImageLoadingListener() { 
      @Override 
      public void onLoadingStarted(String s, View view) { 
      progressBar.setVisibility(View.VISIBLE); 
      } 

      @Override 
      public void onLoadingFailed(String s, View view, FailReason failReason) { 
       progressBar.setVisibility(View.GONE); 
      } 

      @Override 
      public void onLoadingComplete(String s, View view, Bitmap bitmap) { 
       progressBar.setVisibility(View.GONE); 
      } 

      @Override 
      public void onLoadingCancelled(String s, View view) { 
       progressBar.setVisibility(View.GONE); 
      } 
      }); 



       holder.tvUnvan.setText("Unvan: " +     personelModelList.get(position).getUnvan()); 
       holder.tvAd.setText("Ad: " + personelModelList.get(position).getAd()); 
       holder.tvSoyad.setText("Soyad: " + personelModelList.get(position).getSoyad()); 
       holder.tvOda.setText("Oda: " + personelModelList.get(position).getOda()); 




       return convertView; 
      } 
      } 


    class ViewHolder 

    { 

     private ImageView ivResim; 
     private TextView tvAd; 
     private TextView tvSoyad; 
     private TextView tvOda; 
     private TextView tvUnvan; 

    } 

    } 

回答

2

您必須使用onCreateView這樣,因爲你在你在你的logcat的63號線越來越NullPointerException,因爲你的getView()方法返回null;你充氣根佈局之前,使用getView()的Cuz:

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_dahiliye, container, false); 
    //then use findViewById like that because you getting nullpointerexception 
    lvPersonel = (ListView) rootView.findViewById(R.id.lvPersonel); 
    return rootView; 
} 
3

根據您的logcat的結果, 你可以參考DahiliyeFragment類的63號線, 它提到一個變量調用findViewById的空指針異常(..)根據你的代碼,你有

lvPersonel = (ListView) getView().findViewById(R.id.lvPersonel); 

由此看來,由於getView()沒有啓動,因爲您處於onCreateView(..)狀態,這意味着getView()將是一個空對象,這就是返回空指針異常的原因。

爲了解決這個問題,你可以申請

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_dahiliye, container, false); 

    new JSONTask().execute("https://api.myjson.com/bins/4hopr"); 

    DisplayImageOptions defaultOptions = new    DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build(); 
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(  getActivity()).defaultDisplayImageOptions(defaultOptions) 
    .build(); 
    ImageLoader.getInstance().init(config); // Do it on Application start 

    lvPersonel = (ListView) v.findViewById(R.id.lvPersonel); 


    return v; 
} 
+0

是的,它的工作原理是片段感謝,但其他片段有一個錯誤「未找到ID看法?」我不知道什麼是錯的原因都有相同的代碼 –

+0

首先, 請確保你有R.layout.fragment_dahiliye和R.id.lvPersonel在你的資源文件 然後, 請確保你沒有導入android.R在類中有錯誤 –

1

在烏爾onCreateView方法中,首先膨脹視圖和找到烏爾的ListView lvPersonel膨脹之後。例如,

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_dahiliye, container, false); 
    new JSONTask().execute("https://api.myjson.com/bins/4hopr"); 

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build(); 
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).defaultDisplayImageOptions(defaultOptions) 
    .build(); 
    ImageLoader.getInstance().init(config); // Do it on Application start 

    lvPersonel = (ListView) getView().findViewById(R.id.lvPersonel); 


    return view; 
}