Listview排球与碎片

时间:2015-02-27 04:35:14

标签: java android listview adapter

我想在片段中显示listview,但是我收到了错误。

这是我的 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);
}

我收到了这个错误: java.lang中。的的NullPointerException

我使用Log.d进行调试,错误行在:

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

是什么?

这是我的 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):致命异常:主要   02-27 04:45:26.808:E / AndroidRuntime(4739):java.lang.RuntimeException:无法启动活动ComponentInfo {com.koenb_fashion_fix / com.koenb_fashion_fix.MainActivity}:java.lang.NullPointerException   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.os.Handler.dispatchMessage(Handler.java:99)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.os.Looper.loop(Looper.java:137)   02-27 04:45:26.808:E / AndroidRuntime(4739):在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):引起: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):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.BackStackRecord.run(BackStackRecord.java:635)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.Activity.performStart(Activity.java:5017)   02-27 04:45:26.808:E / AndroidRuntime(4739):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)   02-27 04:45:26.808:E / AndroidRuntime(4739):... 11更多

1 个答案:

答案 0 :(得分:1)

下面:

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

使用findViewById调用getActivity()方法意味着从添加了Fragment的活动布局访问视图。

使用rootView中的onCreateView对象访问片段布局中的视图:

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