在自定义ListView适配器中获取NullPointerException

时间:2012-05-16 16:23:23

标签: android listview javamail custom-adapter

我在lv.setAdapter(adapter)上收到NullPointerException。我不明白,我更改了适配器中的一些代码,但后来我删除了它们。然后开始这个例外。

在kimdenlistduzeltilmis:4值

enter image description here

在konulist:4值

enter image description here

final LayoutInflater mLInflater = getLayoutInflater();
        final ListViewAdapter adapter = new ListViewAdapter(
                getApplicationContext(), kimdenlistduzeltilmis, konulist,
                mLInflater);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View item,
                    int position, long id) {
                // adapter.Refresh();
                onClickPosition = position;
                try {
                    EkDurumu ekdurumuNesnesi = new EkDurumu();
                    ekdurumuNesnesi.MessagePositionEkBulunuyormu(
                            onClickPosition, goruntuleNesnesi.getMessages());

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

ListViewAdapter:

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ListViewAdapter extends BaseAdapter {
    static HashMap<Integer, Boolean> cartItems = new HashMap<Integer, Boolean>();
    Context mContext;
    ArrayList<String> kimdenlist; // to load images
    ArrayList<String> konulist; // for data
//  ArrayList<String> imagelist;
    LayoutInflater mLayoutInflater;

    public ListViewAdapter(Context context, ArrayList<String> kimdenlist, ArrayList<String> konulist,
            LayoutInflater layoutInflater) {
        mContext = context;
        this.kimdenlist = kimdenlist;
        this.konulist = konulist;
        mLayoutInflater = layoutInflater;
    }

    @Override
    public int getCount() 
    {
        return konulist.size(); // images array length
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    int count = 0;

    // customized Listview
    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {

        View v;
        final int pos = position;
        v = mLayoutInflater.inflate(R.layout.listust, null);

        TextView kimden = (TextView) v.findViewById(R.id.textvKimden);
        kimden.setText(kimdenlist.get(position));
        TextView konu = (TextView) v.findViewById(R.id.textvKonu);
        konu.setText(konulist.get(position));

//      // saving check box state at the time of raloading
        CheckBox ch = (CheckBox) v.findViewById(R.id.chk);
        try {
            if (count != 0) {
                boolean b = cartItems.get(pos);
                if (b == false)
                    ch.setChecked(false);
                else
                    ch.setChecked(true);
            }
        } catch (NullPointerException e) {

        }


        ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                cartItems.put(pos, arg1);
                count++;

            }
        });
        return v;
    }

    public static HashMap<Integer, Boolean> getcartItems() {
        return cartItems;
    }

    public void Remove()
       {
        notifyDataSetChanged();
       }

}

我的Logcat:

05-16 16:13:12.962: E/AndroidRuntime(347): FATAL EXCEPTION: main
05-16 16:13:12.962: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobil.eposta/com.mobil.eposta.ListeleActivity}: java.lang.NullPointerException
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.os.Looper.loop(Looper.java:123)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 16:13:12.962: E/AndroidRuntime(347):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 16:13:12.962: E/AndroidRuntime(347):  at java.lang.reflect.Method.invoke(Method.java:521)
05-16 16:13:12.962: E/AndroidRuntime(347):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 16:13:12.962: E/AndroidRuntime(347):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 16:13:12.962: E/AndroidRuntime(347):  at dalvik.system.NativeStart.main(Native Method)
05-16 16:13:12.962: E/AndroidRuntime(347): Caused by: java.lang.NullPointerException
05-16 16:13:12.962: E/AndroidRuntime(347):  at com.mobil.eposta.ListeleActivity.onCreate(ListeleActivity.java:109)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 16:13:12.962: E/AndroidRuntime(347):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-16 16:13:12.962: E/AndroidRuntime(347):  ... 11 more

1 个答案:

答案 0 :(得分:1)

看起来您的ListView为空。尝试添加

lv = (ListView) findViewById(R.id.whatever_id_you_gave_your_listview);

在调用setAdapter()

之前