2016-08-04 8 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
あなた getView()メソッドはnullを返しますので、あなたは、あなたのlogcatであなたのライン63で NullPointerExceptionを得るためにあなたは、そのような onCreateViewを使用する必要があります

。あなたは、ルートレイアウトを膨張させる前に、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を呼び出し、変数のnullポインタ例外に言及(..)関数

コードによれば、

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

これは、getView()がnullオブジェクトになることを意味するonCreateView(..)状態にあるようにgetView()が開始されないため、nullポインタ例外を戻す理由です。

これを解決するには、あなたが適用される場合があります

@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

はい、それはこのフラグメントのおかげで動作しますが、他のフラグメントは?私はどちらも同じコード –

+0

まず、 を持っている間違った原因は確認してください何であるかエラー「IDが見つかりませんビュー」を知っていないしていますあなたのリソースファイルに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; 
} 
関連する問題