2 hcg 21 hcg_21 于 2013.01.09 11:10 提问

ArrayAdapter报出NullPointerException异常

在Activity中有一个listview。listview中使用arrayAdapter布局填充。array Adapter是从Activity中调用的。Activity传递一个arrayList对象到adapter。当执行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();  
 }   

    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);    
}

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();  
    } 

相关异常:

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: 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.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)

2个回答

CSDN_Ask
CSDN_Ask   2013.01.09 14:25
已采纳

程序中的 listview 必须是空的。在 activity 中使用正确的布局文件了吗?
或者程序中的adapterTicketList仍然是空的。

myc_100
myc_100   2013.01.10 14:07

看似你的listview是没有赋值的。在listView.setAdapter(adapterTicketList);处放一个断点,然后检查listview的值。
确保要导入正确的R文件,并清除程序的缓存。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!