Mihriban Tekeş Mihriban Tekeş - 4 months ago 11
Android Question

How to show json data in sliding menu fragments?

I want to show json data in sliding menu fragments but when i entered the fragment app closes unexpectedly, i have 34 fragments and i need to show different Json datas each fragment . Thanks for helping i hope you understand the problem.

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;

}

}

Answer

You must use onCreateView like that, because you getting NullPointerException at your Line 63 in your logcat, because your getView() method returns null ; cuz you using getView() before inflating root layout:

@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;
}