0

我正在尝试使用数组适配器来填充我的列表视图。我构建了一个从 POJO 类到数组适配器的对象数组列表。当我尝试通过传递对象的arraylist 来实例化arrayadapter 类时,我在执行SETADAPTER 行时遇到空指针异常。

请指导我解决这个问题

private void initialize() {     
    listView=(ListView)findViewById(R.id.lvOpenTicketList);

    POJO_Ticket pojo_Ticket; 
    ArrayList<POJO_Ticket> ticketArray  = new ArrayList<POJO_Ticket>(); 

     for(int i=0;i<openTicketList.size();i++){ 
        pojo_Ticket = new POJO_Ticket(openTicketList.get(i));
        ticketArray.add(pojo_Ticket);    
        Log.i("OpenTicket","i = " + Integer.toString(i)); 
     }
        Log.i("OpenTicket","ticketArray  " + ticketArray.size()); 


        adapterTicketList=new AdapterTicketList(this, ticketArray);
        listView.setAdapter(adapterTicketList);
        listView.setOnItemClickListener(this);    
}

public class AdapterTicketList extends ArrayAdapter<POJO_Ticket> {
Context context;
ArrayList<POJO_Ticket> ticketArray;
LayoutInflater vi;

public AdapterTicketList(Context context , ArrayList<POJO_Ticket> ticketArray) {    
    super(context, 0, ticketArray );    
    this.ticketArray = new ArrayList<POJO_Ticket>();    
    this.ticketArray.addAll(ticketArray);  
    this.context =context;
    Log.i("AdapterTicketList"," ticket id : " + ticketArray.get(0).getTicketObject().getId()); 
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }  

private class ViewHolder {
    TextView tvTicketID;  
    TextView tvCreationHour;  
    TextView tvTableNumber;  
    TextView tvAmount;    
    } 

public View getView(final int position, View convertView, ViewGroup parent) {      
    ViewHolder holder;    
    if (convertView == null) { 
        holder = new ViewHolder();                   
        convertView = vi.inflate(R.layout.rowticket,  null);  
        holder.tvTicketID = (TextView) convertView.findViewById(R.id.tvTicketID); 
        holder.tvCreationHour = (TextView) convertView.findViewById(R.id.tvCreationHour); 
        holder.tvTableNumber = (TextView) convertView.findViewById(R.id.tvTableNumber); 
        holder.tvAmount = (TextView) convertView.findViewById(R.id.tvQuantity);  
        convertView.setTag(holder); 
    } else {
        holder = (ViewHolder) convertView.getTag();  
    }       
    Log.i("AdapterTicketList"," ticket id : " + ticketArray.get(position).getTicketObject().getId()); 
    holder.tvTicketID.setText(ticketArray.get(position).getTicketObject().getId()) ;  
    holder.tvCreationHour.setText(ticketArray.get(position).getTicketObject().getCreationHour()) ;  
    holder.tvTableNumber.setText(ticketArray.get(position).getTicketObject().getTableNumber()) ;   
    holder.tvAmount.setText(Double.toString(ticketArray.get(position).getTicketObject().getTotalAmount())) ; 
    return convertView; 
    } 

}

01-08 22:11:13.707: W/dalvikvm(7284): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-08 22:11:13.717: E/AndroidRuntime(7284): FATAL EXCEPTION: main
01-08 22:11:13.717: E/AndroidRuntime(7284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.restaurent/com.example.restaurent.OpenTicketList}: java.lang.NullPointerException
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.os.Looper.loop(Looper.java:137)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-08 22:11:13.717: E/AndroidRuntime(7284): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 22:11:13.717: E/AndroidRuntime(7284): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 22:11:13.717: E/AndroidRuntime(7284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-08 22:11:13.717: E/AndroidRuntime(7284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-08 22:11:13.717: E/AndroidRuntime(7284): at dalvik.system.NativeStart.main(Native Method)
01-08 22:11:13.717: E/AndroidRuntime(7284): Caused by: java.lang.NullPointerException
01-08 22:11:13.717: E/AndroidRuntime(7284): at com.example.restaurent.OpenTicketList.initialize(OpenTicketList.java:67)
01-08 22:11:13.717: E/AndroidRuntime(7284): at com.example.restaurent.OpenTicketList.onCreate(OpenTicketList.java:48)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.Activity.performCreate(Activity.java:4465)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-08 22:11:13.717: E/AndroidRuntime(7284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-08 22:11:13.717: E/AndroidRuntime(7284): ... 11 more
4

1 回答 1

1
Caused by: java.lang.NullPointerException
    at com.example.restaurent.OpenTicketList.initialize(OpenTicketList.java:67)

NullPointerException 意味着您试图从空变量引用方法或成员,在这种情况下null.setAdapter()

这意味着您当前的布局没有 ID 为 ListView lvOpenTicketList...

于 2013-01-08T18:11:46.373 回答