在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)
程序中的 listview 必須是空的。在 activity 中使用正確的布局文件了嗎?
或者程序中的adapterTicketList
仍然是空的。