Xenix Putra Sasongko Xenix Putra Sasongko - 4 months ago 15
Android Question

Listview Volley with Fragment

I want to show

listview
in fragment, but I got an error.

This is my MainActivity:

public class HomeFragment extends Fragment {
private List<Produk> produkList = new ArrayList<Produk>();
private ListView listView;
private CustomListAdapter adapter;
private ProgressDialog pDialog;
private ServerRequest serverRequest;
JSONArray member = null;
private static final String url = "http://10.0.3.2:808/Koen_CI/index.php/daftar_barang_control";

public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loadPreferences();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Log.d("TAG", "TEST1");
listView = (ListView) getActivity().findViewById(R.id.list);
Log.d("TAG", "TEST2");
adapter = new CustomListAdapter(getActivity(), produkList);
Log.d("TAG", "TEST3");
listView.setAdapter(adapter);
Log.d("TAG", "TEST4");

// Creating volley request obj
JsonArrayRequest produkReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("TAG", response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {

JSONObject obj = response.getJSONObject(i);
Produk produk = new Produk();
produk.setNamaProduk(obj.getString("nama_produk"));
produk.setHargaProduk(obj.getString("harga_produk"));
produk.setFotoProduk(obj.getString("foto_produk"));
Log.d("TAG", "TAG : " + produk.getNamaProduk());

// adding movie to movies array
produkList.add(produk);

} catch (JSONException e) {
e.printStackTrace();
}

}

// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
hidePDialog();

}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(produkReq);
}


I got this error:
java.lang.NullPointerException

I debug with Log.d, error line is in:

adapter = new CustomListAdapter(getActivity(), produkList);


what is it?

this is my CustomListAdapter

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Produk> produkItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

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

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

@Override
public Object getItem(int location) {
return produkItems.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 title = (TextView) convertView.findViewById(R.id.namaProduk);
TextView rating = (TextView) convertView.findViewById(R.id.hargastokProduk);

// getting movie data for the row
Produk p = produkItems.get(position);

// thumbnail image
thumbNail.setImageUrl(p.getFotoProduk(), imageLoader);

// title
title.setText(p.getNamaProduk());

// rating
rating.setText("Harga: " + p.getHargaProduk());

return convertView;
}

}


Logcat:


02-27 04:45:26.808: E/AndroidRuntime(4739): FATAL EXCEPTION: main
02-27 04:45:26.808: E/AndroidRuntime(4739): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.koenb_fashion_fix/com.koenb_fashion_fix.MainActivity}: java.lang.NullPointerException
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.os.Looper.loop(Looper.java:137)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-27 04:45:26.808: E/AndroidRuntime(4739): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 04:45:26.808: E/AndroidRuntime(4739): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 04:45:26.808: E/AndroidRuntime(4739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-27 04:45:26.808: E/AndroidRuntime(4739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 04:45:26.808: E/AndroidRuntime(4739): at dalvik.system.NativeStart.main(Native Method)
02-27 04:45:26.808: E/AndroidRuntime(4739): Caused by: java.lang.NullPointerException
02-27 04:45:26.808: E/AndroidRuntime(4739): at com.koen_bfashion.image.CustomListAdapter.(CustomListAdapter.java:22)
02-27 04:45:26.808: E/AndroidRuntime(4739): at com.koenb_fashion_fix.Home_Fragment.onCreate(Home_Fragment.java:72)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.BackStackRecord.run(BackStackRecord.java:635)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.Activity.performStart(Activity.java:5017)
02-27 04:45:26.808: E/AndroidRuntime(4739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
02-27 04:45:26.808: E/AndroidRuntime(4739): ... 11 more

Answer

Here:

listView = (ListView) getActivity().findViewById(R.id.list);

Calling findViewById method using getActivity() means accessing view from layout of Activity in which Fragment is added.

Use rootView object in onCreateView to access views from layout of Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {         
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    listView = (ListView) rootView.findViewById(R.id.list);
    return rootView;
}
Comments