android.widget.LinearLayout所有可能的错误类型

时间:2013-10-14 03:30:42

标签: android listview android-linearlayout

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包含2个TextViews和一个复选框。此layout用于生成ListView

1 个答案:

答案 0 :(得分:1)

您好像没有正确使用findViewById()。如果DEVNAME中的DEVADDREXEMPTidcustom_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()的{​​{1}}。{/ p>

为了清晰起见而编辑

From the Docs

  

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

所以你这样做的方法是查看最初用layout充气的setContentView()。现在您已经夸大了另一个xml文件,该文件包含您尝试访问的View,因此您需要在layout之前放置新膨胀findViewById()的变量,以便应用知道查看 layout而不是最初处理过的layout