-1

我有一个 android 代码,但总是error : Nullpointerexception在加载这个listview和时adapter,这是loadlist方法fragment

加载列表是第 285 行

private void loadList() {
        // TODO Auto-generated method stub
        resultList = getListData();

        final ListView listView = (ListView) rootView.findViewById(R.id.pbb_data_op_list);
        listView.setAdapter(new LogListAdapter(rootView.getContext(), createDummyData()));

    }

这创建虚拟只是列表视图的虚拟

public List<ACTIVITY_LOG> createDummyData() {
        List<ACTIVITY_LOG> logs = new ArrayList<ACTIVITY_LOG>();

        ACTIVITY_LOG log = new ACTIVITY_LOG();
        log.setmMenu(MenuString.MENU_PBB);
        log.setmSubMenu("Verifying");
        log.setmAktifitas("Melakukan Insert Data");
        log.setmDate(new Date());

        ACTIVITY_LOG log2 = new ACTIVITY_LOG();
        log2.setmMenu(MenuString.MENU_X);
        log2.setmSubMenu("DATA OP");
        log2.setmAktifitas("Melakukan Insert Data sofie");
        log2.setmDate(new Date());

        logs.add(log);
        logs.add(log2);


        for (int i = 0; i < 20; i++) {
            int counter = 1200;

            ACTIVITY_LOG log3 = new ACTIVITY_LOG();
            log3.setmMenu(MenuString.MENU_X);
            log3.setmSubMenu("DATA OBJEK PAJAK");
            log3.setmAktifitas("Melakukan Update Data "
                    + (counter + i) + ".0");
            log3.setmDate(new Date());

            logs.add(log3);
        }


        return logs;
    }

这是日志列表活动:

public class LogListAdapter extends BaseAdapter {

    private Context thisContext;
    private List<ACTIVITY_LOG> thisListData;
    private LayoutInflater layoutInflater;

    public LogListAdapter(Context context, List listData) {
        // TODO Auto-generated constructor stub
        thisContext = context;
        thisListData = listData;
        layoutInflater = LayoutInflater.from(thisContext);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return thisListData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return thisListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.log_list_item_layout,
                    null);
            holder = new ViewHolder();
            holder.gambarMenuView = (ImageView) convertView
                    .findViewById(R.id.gambar_menu);
            holder.subMenuView = (TextView) convertView
                    .findViewById(R.id.sub_menu_nama);
            holder.aktifitasView = (TextView) convertView
                    .findViewById(R.id.aktifitas_nama);

            holder.tanggalView = (TextView) convertView
                    .findViewById(R.id.aktifitas_tanggal);

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

        int menuId = thisListData.get(position).getmMenu();

        holder.gambarMenuView.setImageResource(MenuString.getMenuImage(menuId));
        holder.subMenuView.setText(thisListData.get(position).getmSubMenu());
        holder.aktifitasView
                .setText(thisListData.get(position).getmAktifitas());
        holder.tanggalView
                .setText(thisListData.get(position).getmDate() != null ? thisListData
                        .get(position).getmDate().toString()
                        : "");
        return convertView;
    }

    static class ViewHolder {
        ImageView gambarMenuView;
        TextView subMenuView;
        TextView aktifitasView;
        TextView tanggalView;

    }

}

这是 logcat :

09-02 16:20:08.646: E/AndroidRuntime(14848): FATAL EXCEPTION: main
09-02 16:20:08.646: E/AndroidRuntime(14848): java.lang.NullPointerException
09-02 16:20:08.646: E/AndroidRuntime(14848):    at com.imotax.fragments.pbb.PBBDataOPFormLSPOPFragment.loadList(PBBDataOPFormLSPOPFragment.java:285)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at com.imotax.fragments.pbb.PBBDataOPFormLSPOPFragment.access$0(PBBDataOPFormLSPOPFragment.java:278)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at com.imotax.fragments.pbb.PBBDataOPFormLSPOPFragment$1.onItemSelected(PBBDataOPFormLSPOPFragment.java:141)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.widget.AdapterView.access$200(AdapterView.java:49)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.os.Handler.handleCallback(Handler.java:615)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.os.Looper.loop(Looper.java:153)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at android.app.ActivityThread.main(ActivityThread.java:4987)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at java.lang.reflect.Method.invokeNative(Native Method)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at java.lang.reflect.Method.invoke(Method.java:511)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-02 16:20:08.646: E/AndroidRuntime(14848):    at dalvik.system.NativeStart.main(Native Method)

我不知道为什么,但是如果我在其他活动中创建它而不是像这个问题这样的 loadlist 方法,它就可以工作。有原因或解决方案吗?

4

1 回答 1

0

您绝对应该放置异常的堆栈跟踪,以便我们可以看到错误在哪里。

同时,在 getView 方法中膨胀列表项时出现错误。更改自:

convertView = layoutInflater.inflate(R.layout.log_list_item_layout, null);

至:

convertView = layoutInflater.inflate(R.layout.log_list_item_layout, parent, false);

编辑: loadList 方法中的哪一行是 285?并且您可能会考虑使用 getActivity() 而不是 rootView.getContext().getApplicationContext() 因为您要在片段中加载列表

于 2013-09-02T09:23:16.360 回答