0
public class ExemptListView extends Activity {

DbHandler dbHandler;
Cursor c;
ArrayList<Devices> list;
MycustomAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    //final ListView listview_var = (ListView) findViewById(R.id.listView1);
    dbHandler = new DbHandler(getApplicationContext());

    list = new ArrayList<Devices>();
    generateListofDevices();
    for (int i = 0; i < list.size(); i++) {
        Log.i("Dhiraj", list.get(i).devicename + " : "
                + list.get(i).IpAddress + " : " + list.get(i).checked);

    }

    adapter = new MycustomAdapter(this, android.R.layout.simple_list_item_1,list);

    ListView lv=(ListView)findViewById(R.id.listView1);
    lv.setAdapter(adapter);
    Log.i("Dhiraj"," Adapter set");

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.i("Dhiraj","lv.setitemclicklistener");
            Devices dv=(Devices)arg0.getItemAtPosition(arg2);
             //Toast.makeText(getApplicationContext(),
                //      "Clicked on Row: " + dv.devicename,
                  //    Toast.LENGTH_LONG).show();              
        }
    });

    checkbuttonclick();
}

private void generateListofDevices() {
    c = dbHandler.GetCursor(dbHandler.Devices_List_db);
    Log.i("Dhiraj", "generating lsit ");
    if (c != null) {
        if (c.moveToFirst())
            do {
                Boolean Checked = false;
                Log.i("Dhiraj", c.getString(1) + " : " + c.getString(2)
                        + " : " + c.getString(3));
                if (c.getString(3) == "Y")
                    Checked = true;
                Devices d = new Devices(c.getString(1), c.getString(2),
                        Checked);
                list.add(d);
            } while (c.moveToNext());
    }
}

public class Devices {
    String devicename = null;
    String IpAddress = null;
    Boolean checked = false;

    Devices(String devicename, String IpAddress, Boolean checked2) {
        this.devicename = devicename;
        this.IpAddress = IpAddress;
        this.checked = checked2;
    }

    public void setSelected(boolean selected) {
        this.checked = selected;
    }

}

private class MycustomAdapter extends ArrayAdapter<Devices> {
    private ArrayList<Devices> Deviceslist;

    public MycustomAdapter(Context context, int resource,
            ArrayList<Devices> DeviceList) {
        super(context, resource, DeviceList);
        this.Deviceslist = new ArrayList<Devices>();
        this.Deviceslist.addAll(DeviceList);
        Log.i("Dhiraj","MycustomAdapter constructor cmpleted");
    }

    private class ViewHolder {
        TextView Name;
        TextView IP;
        CheckBox Exempt;            
    }

    public View getView(int position, View convertview, ViewGroup parent) {
        Log.i("Dhiraj"," GET View");
        ViewHolder holder = null;

        if (convertview == null) {
            Log.i("Dhiraj","convertview is null");
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertview = vi.inflate(R.layout.custom_checkboxlist, null);

            Log.i("Dhiraj", convertview.toString());
            Log.i("Dhiraj","NEw Holder");
            holder = new ViewHolder();
            holder.Name = (TextView) findViewById(R.id.DEVNAME);
            holder.IP = (TextView) findViewById(R.id.DEVADDR);
            holder.Exempt = (CheckBox) findViewById(R.id.EXEMPT);
            convertview.setTag(holder);

            Log.i("Dhiraj","convertview.setTag(holder)");
            holder.Exempt.setOnClickListener(new OnClickListener() {                    
                @Override
                public void onClick(View v) {

                    Log.i("Dhiraj"," on click listener");

                    CheckBox cb = (CheckBox) v;
                    Devices devices = (Devices) cb.getTag();

                    Toast.makeText(
                            getApplicationContext(),
                            "Clicked on Checkbox: " + cb.getText() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG)
                            .show();
                    devices.setSelected(cb.isChecked());
                }
            });
        }           
        else
        {
            Log.i("Dhiraj","Else");
            holder=(ViewHolder)convertview.getTag();
        }

        Log.i("Dhiraj","Done with null and else");

        Devices device=Deviceslist.get(position);Log.i("Dhiraj",Deviceslist.get(position)+"");
        holder.Name.setText(device.devicename);Log.i("Dhiraj",device.devicename);
        holder.IP.setText(device.IpAddress);Log.i("Dhiraj",device.IpAddress);
        holder.Exempt.setChecked(device.checked);Log.i("Dhiraj",device.checked.toString());
        Log.i("Dhiraj"," View Over");
        return convertview;
    }
}

public void checkbuttonclick(){
    Button Apply=(Button)findViewById(R.id.Apply);
    Log.i("Dhiraj","chcek button");
    ArrayList<Devices> deviceslist=adapter.Deviceslist;
    for(int i=0;i<deviceslist.size();i++){
        Devices d=deviceslist.get(i);
        if(d.checked)
        Log.i("Dhiraj", d.devicename +" is Exempted");
    }
}

}

logcat 显示以下错误

10-14 08:51:18.486: I/Dhiraj(25481): Database Name: DevicesList
10-14 08:51:18.494: I/Dhiraj(25481): returning all Values
10-14 08:51:18.494: I/Dhiraj(25481): generating lsit 
10-14 08:51:18.494: I/Dhiraj(25481): Ujgffbb : Vhncfcv : N
10-14 08:51:18.494: I/Dhiraj(25481): Uj : Vh : N
10-14 08:51:18.494: I/Dhiraj(25481): Ihdecv : Ibswchj : N
10-14 08:51:18.494: I/Dhiraj(25481): Ih : Ib : N
10-14 08:51:18.494: I/Dhiraj(25481): Hhbb : Plhb : N
10-14 08:51:18.494: I/Dhiraj(25481): Ujgffbb : Vhncfcv : false
10-14 08:51:18.494: I/Dhiraj(25481): Uj : Vh : false
10-14 08:51:18.494: I/Dhiraj(25481): Ihdecv : Ibswchj : false
10-14 08:51:18.494: I/Dhiraj(25481): Ih : Ib : false
10-14 08:51:18.502: I/Dhiraj(25481): Hhbb : Plhb : false
10-14 08:51:18.502: I/Dhiraj(25481): MycustomAdapter constructor cmpleted
10-14 08:51:18.502: I/Dhiraj(25481):  Adapter set
10-14 08:51:18.502: I/Dhiraj(25481): chcek button
10-14 08:51:18.518: I/Dhiraj(25481):  GET View
10-14 08:51:18.518: I/Dhiraj(25481): convertview is null
10-14 08:51:18.518: I/Dhiraj(25481): android.widget.LinearLayout@41418e10
10-14 08:51:18.518: I/Dhiraj(25481): NEw Holder
10-14 08:51:18.518: I/Dhiraj(25481): convertview.setTag(holder)
10-14 08:51:18.525: W/dalvikvm(25481): threadid=1: thread exiting with uncaught exception (group=0x40ab5210)
10-14 08:51:18.533: E/AndroidRuntime(25481): FATAL EXCEPTION: main
10-14 08:51:18.533: E/AndroidRuntime(25481): java.lang.NullPointerException
10-14 08:51:18.533: E/AndroidRuntime(25481):    at com.dhiraj.projectsierra_v1.ExemptListView$MycustomAdapter.getView(ExemptListView.java:136)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.AbsListView.obtainView(AbsListView.java:2144)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.ListView.onMeasure(ListView.java:1155)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.View.measure(View.java:12751)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.View.measure(View.java:12751)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:295)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.View.measure(View.java:12751)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.View.measure(View.java:12751)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:295)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2120)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.View.measure(View.java:12751)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1106)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2558)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.os.Looper.loop(Looper.java:137)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at android.app.ActivityThread.main(ActivityThread.java:4697)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at java.lang.reflect.Method.invoke(Method.java:511)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-14 08:51:18.533: E/AndroidRuntime(25481):    at dalvik.system.NativeStart.main(Native Method)

我没有弄错它。中是否有错误Layout。我正在使用一个LinearLayout包含 2TextViews和一个复选框的。这layout用于生成ListView

4

1 回答 1

1

看起来你没有findViewById()正确使用。如果DEVNAME, DEVADDR, and EXEMPTare ids insidecustom_checkboxlist.xml那么它们应该用类似的东西来引用

 holder.Name = (TextView) convertview.findViewById(R.id.DEVNAME);
 holder.Name = (TextView) convertview.findViewById(R.id.DEVADDR);
 holder.Name = (TextView) convertview.findViewById(R.id.EXEMPT);

你需要看看那个膨胀的内部layout。否则,他们正在寻找先前膨胀layout的方向setContentView()

为清晰起见进行编辑

从文档

从在 onCreate(Bundle) 中处理的 XML 中查找由 id 属性标识的视图

因此,您这样做的方式是查看layout最初用setContentView(). 现在您已经膨胀了另一个 xml 文件,该文件包含View您尝试访问的 s ,因此您需要将新膨胀的变量放在layout之前,findViewById()以便应用程序知道查看其中 layout而不是最初处理的layout.

于 2013-10-14T03:49:34.130 回答