0

ArrayAdapter を使用して生成された Lsit ビューがあります。getView メソッドの TextView フィールドは、POJO クラスからの値を入力しているときにエラーをスローしています。コードの修正が必要な場所を教えてください。

前もって感謝します。

public class ApplianceAdapter extends ArrayAdapter<POJO_Appliance> {
    private ArrayList<POJO_Appliance> appliance;  
    Context context;
    LayoutInflater vi; 
    public ApplianceAdapter(Context context ,ArrayList<POJO_Appliance> appliance) {    
        super(context, 0, appliance); 
        this.appliance = new ArrayList<POJO_Appliance>();    
        this.appliance.addAll(appliance); 
        this.context =context;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }     
    private class ViewHolder {
        TextView applianceName;     
        Button cancel,edit; 
    } 
    public View getView(final int position, View convertView, ViewGroup parent) {      
        ViewHolder holder;  
        if (convertView == null) { 
            holder = new ViewHolder();  
                convertView    = vi.inflate(R.layout.rowappliance,  null); 
                holder.applianceName    = (TextView) convertView.findViewById(R.id.tvapplianceName);  
                holder.cancel  = (Button)   convertView.findViewById(R.id.bCancel); 
                holder.edit    = (Button)   convertView.findViewById(R.id.bEdit);        
            convertView.setTag(holder); 
        } else {
            holder = (ViewHolder) convertView.getTag();  
        }  

        Log.i("AddAppliance"," Appliance Name : " + appliance.get(position).getApplianceName());
        holder.applianceName.setText(appliance.get(position).getApplianceName());   

        //   Cancel button
        holder.cancel.setTag(position); 
        holder.cancel.setOnClickListener(new View.OnClickListener() {        
            public void onClick(View v) {                   
                try {
                    int pos = (Integer) v.getTag();                         
                    Log.i("ConfirmAdapter ","Order cancel @ position : " + pos);  
                    entry.deleteAppliance(roomName,appliance.get(pos).getApplianceName());
                    refreshAdapter();
                    } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    }  
                }       
            });    
        return convertView; 
        } 
}   

エラーログ:

01-18 17:28:32.259: E/AndroidRuntime(28641): FATAL EXCEPTION: main
01-18 17:28:32.259: E/AndroidRuntime(28641): java.lang.NullPointerException
01-18 17:28:32.259: E/AndroidRuntime(28641): at com.example.homeauto.AddAppliance$ApplianceAdapter.getView(AddAppliance.java:136)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.makeAndAddView(ListView.java:1772)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.fillDown(ListView.java:672)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.fillFromTop(ListView.java:732)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.layoutChildren(ListView.java:1611)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.AbsListView.onLayout(AbsListView.java:1870)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1641)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1417)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.os.Looper.loop(Looper.java:137)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-18 17:28:32.259: E/AndroidRuntime(28641): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:28:32.259: E/AndroidRuntime(28641): at java.lang.reflect.Method.invoke(Method.java:511)
01-18 17:28:32.259: E/AndroidRuntime(28641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 17:28:32.259: E/AndroidRuntime(28641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 17:28:32.259: E/AndroidRuntime(28641): at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

2

findViewByIdの参照が間違っている可能性があります。テキストビューまたはレイアウトビューのIDを確認する必要があります:-)

于 2014-12-25T12:38:01.367 に答える