2

我有一个有列表视图的活动。使用 arrayAdapter 填充列表视图。数组适配器是从 Activity 中调用的。活动将对象的arrayList 传递给适配器。当我执行 Activity 时,我得到空指针异常。请您指导一下需要更正的地方。

public class OpenTicketList extends Activity  implements  OnItemClickListener{  
private Bundle basket;
private AdapterTicketList adapterTicketList;
private List<Ticket> openTicketList;
private ListView listView;

@TargetApi(9)
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
     Log.i("OpenTicket","on create"); 
     setContentView(R.layout.openticketlist);
     catchBasket(); 
     initialize();  
 }   

//---------------------------------------------------------------------------------------------------------------------
//---To initialize , Bind the widgets and create Tabs
//--------------------------------------------------------------------------------------------------------------------- 
private void initialize() {     
    listView=(ListView)findViewById(R.id.lvOpenTicketList);

    POJO_Ticket pojo_Ticket; 
    ArrayList<POJO_Ticket> ticketArray;
    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)); 
     }
        adapterTicketList=new AdapterTicketList(this, ticketArray);
        listView.setAdapter(adapterTicketList);
        listView.setOnItemClickListener(this);    
}
//---------------------------------------------------------------------------------------------------------------------
//---To catch the values from the calling Activity
//---------------------------------------------------------------------------------------------------------------------
private void catchBasket() {
    // TODO Auto-generated method stub
    basket = getIntent().getExtras(); 
    openTicketList = (List<Ticket>) basket.getSerializable("ticket");  
}  

}

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 23:01:55.287: E/AndroidRuntime(7791): FATAL EXCEPTION: main
01-08 23:01:55.287: E/AndroidRuntime(7791): android.content.res.Resources$NotFoundException: String resource ID #0x11
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.content.res.Resources.getText(Resources.java:247)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.TextView.setText(TextView.java:3495)
01-08 23:01:55.287: E/AndroidRuntime(7791): at com.example.restaurent.AdapterTicketList.getView(AdapterTicketList.java:52)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.ListView.makeAndAddView(ListView.java:1772)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.ListView.fillDown(ListView.java:672)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.ListView.fillFromTop(ListView.java:732)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.ListView.layoutChildren(ListView.java:1625)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.AbsListView.onLayout(AbsListView.java:1870)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.View.layout(View.java:11418)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1641)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.onLayout(LinearLayout.java:1417)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.View.layout(View.java:11418)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.View.layout(View.java:11418)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.View.layout(View.java:11418)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.View.layout(View.java:11418)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.View.layout(View.java:11418)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.os.Looper.loop(Looper.java:137)
01-08 23:01:55.287: E/AndroidRuntime(7791): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-08 23:01:55.287: E/AndroidRuntime(7791): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 23:01:55.287: E/AndroidRuntime(7791): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 23:01:55.287: E/AndroidRuntime(7791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-08 23:01:55.287: E/AndroidRuntime(7791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-08 23:01:55.287: E/AndroidRuntime(7791): at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

看起来listViewnull。设置一个断点listView.setAdapter(adapterTicketList);并检查什么是listView值。

确保您正在导入正确的 R 文件并清理您的项目。

于 2013-01-08T16:58:54.350 回答
1

您的列表视图必须为空。您是否在活动中使用了正确的布局文件?

或者你adapterTicketList的仍然是空的。

于 2013-01-08T17:20:49.160 回答
0

您的错误出现在:OpenTicketList.java:67 在我看来,您的“openTicketList”没有任何内容。

很难确定您的问题是什么,但是您正在尝试引用此时尚未实例化的对象。

于 2013-01-08T16:57:38.330 回答