如何避免ArrayIndexOutOfBoundsExceptions.in中的pgm myval和myvals是ArrayList Global Variables.i认为我隐藏GlobalVariable,ArrayList转换为String []只是pbm.plz帮助我。
public class TicketActivity extends Activity{
TextView t1;
TextView t2;
ListView l1;
TextView t6;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket);
t1=(TextView)findViewById(R.id.textView3);
t2=(TextView)findViewById(R.id.textView5);
l1=(ListView)findViewById(R.id.listView1);
//Get the Table no Value From Edit Text
Intent i1=getIntent();
Bundle b=i1.getExtras();
int num=b.getInt("Table No:");
String pno=Integer.toString(num);
t1.setText(pno);
//Get The Guest Value From Edit Text
Intent i2=getIntent();
Bundle b1=i2.getExtras();
int num1=b1.getInt("Guest:");
String pno1=Integer.toString(num1);
t2.setText(pno1);
l1.setAdapter(new EfficientAdapter(TicketActivity.this));
}
private static class EfficientAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public EfficientAdapter(Context context){
mInflater=LayoutInflater.from(context);
// implementation of EfficientAdapter
}
public int getCount() {
return HomeActivity.select1.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position ;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String[] stockArr = new String[GlobalClass.myval.size()];
stockArr = GlobalClass.myval.toArray(stockArr);
String[] stockArr1=new String[GlobalClass.myvals.size()];
stockArr1=GlobalClass.myvals.toArray(stockArr1);
if(convertView==null){
convertView=mInflater.inflate(R.layout.list_ticket, null);
holder=new ViewHolder();
holder.Text1=(TextView) convertView.findViewById(R.id.textView1);
holder.Text2=(TextView) convertView.findViewById(R.id.textView2);
holder.Text3=(TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
holder.Text2.setText(stockArr[position]);
holder.Text3.setText(stockArr1[position]);
return convertView;
}
}
static class ViewHolder{
TextView Text1;
TextView Text2;
TextView Text3;
}
}
LogCat输出:
08-03 10:50:59.417: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:50:59.497: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:22.737: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:23.076: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:23.236: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:23.387: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:23.507: W/System.err(807): java.lang.NullPointerException
08-03 10:52:23.537: W/System.err(807): at com.eConnect.Restaurant.HomeActivity.onCreate(HomeActivity.java:142)
08-03 10:52:23.537: W/System.err(807): at android.app.Activity.performCreate(Activity.java:4465)
08-03 10:52:23.647: W/System.err(807): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-03 10:52:23.647: W/System.err(807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-03 10:52:23.681: W/System.err(807): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-03 10:52:23.696: W/System.err(807): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-03 10:52:23.696: W/System.err(807): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-03 10:52:23.728: W/System.err(807): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 10:52:23.736: W/System.err(807): at android.os.Looper.loop(Looper.java:137)
08-03 10:52:23.776: W/System.err(807): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-03 10:52:23.776: W/System.err(807): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 10:52:23.797: W/System.err(807): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 10:52:23.797: W/System.err(807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-03 10:52:23.827: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:24.107: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:24.196: W/System.err(807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-03 10:52:24.196: W/System.err(807): at dalvik.system.NativeStart.main(Native Method)
08-03 10:52:24.257: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:24.269: D/dalvikvm(807): GC_CONCURRENT freed 100K, 3% free 9379K/9607K, paused 11ms+9ms
08-03 10:52:24.427: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:24.790: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:24.938: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:25.267: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:25.447: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:25.786: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:25.939: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:52:26.286: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:52:26.477: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:53:13.687: I/System.out(807): Length of Breakfast:12
08-03 10:53:13.687: I/System.out(807): Length of P2:12
08-03 10:53:15.837: I/System.out(807): Values[2EGG W/MEAT]
08-03 10:53:15.837: I/System.out(807): Values[$ 4.7]
08-03 10:53:17.248: I/System.out(807): Values[2EGG W/MEAT, BLUEBERRY CAKE]
08-03 10:53:17.248: I/System.out(807): Values[$ 4.7, $ 3.0]
08-03 10:53:21.317: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:53:21.467: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:53:21.817: I/dalvikvm(807): threadid=3: reacting to signal 3
08-03 10:53:21.917: I/dalvikvm(807): Wrote stack traces to '/data/anr/traces.txt'
08-03 10:53:22.087: D/AndroidRuntime(807): Shutting down VM
08-03 10:53:22.087: W/dalvikvm(807): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-03 10:53:22.237: E/AndroidRuntime(807): FATAL EXCEPTION: main
08-03 10:53:22.237: E/AndroidRuntime(807): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
08-03 10:53:22.237: E/AndroidRuntime(807): at com.eConnect.Restaurant.TicketActivity$EfficientAdapter.getView(TicketActivity.java:91)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.ListView.makeAndAddView(ListView.java:1772)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.ListView.fillDown(ListView.java:672)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.ListView.fillFromTop(ListView.java:732)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.ListView.layoutChildren(ListView.java:1625)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.View.layout(View.java:11278)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewGroup.layout(ViewGroup.java:4224)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.View.layout(View.java:11278)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewGroup.layout(ViewGroup.java:4224)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.View.layout(View.java:11278)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewGroup.layout(ViewGroup.java:4224)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.View.layout(View.java:11278)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewGroup.layout(ViewGroup.java:4224)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.View.layout(View.java:11278)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewGroup.layout(ViewGroup.java:4224)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.os.Looper.loop(Looper.java:137)
08-03 10:53:22.237: E/AndroidRuntime(807): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-03 10:53:22.237: E/AndroidRuntime(807): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 10:53:22.237: E/AndroidRuntime(807): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 10:53:22.237: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-03 10:53:22.237: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-03 10:53:22.237: E/AndroidRuntime(807): at dalvik.system.NativeStart.main(Native Method)
08-03 10:53:22.358: I/dalvikvm(807): threadid=3: reacting to signal 3
提前致谢;
答案 0 :(得分:1)
确保HomeActivity.select1.length和GlobalClass.myval.size()以及GlobalClass.myvals.size()具有相同的大小。
我正在编辑你的答案,请记下它只是一个解决方法,因为我需要一个正确的解决方案来理解这些数据源。
public class TicketActivity extends Activity{
TextView t1;
TextView t2;
ListView l1;
TextView t6;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket);
t1=(TextView)findViewById(R.id.textView3);
t2=(TextView)findViewById(R.id.textView5);
l1=(ListView)findViewById(R.id.listView1);
//Get the Table no Value From Edit Text
Intent i1=getIntent();
Bundle b=i1.getExtras();
int num=b.getInt("Table No:");
String pno=Integer.toString(num);
t1.setText(pno);
//Get The Guest Value From Edit Text
Intent i2=getIntent();
Bundle b1=i2.getExtras();
int num1=b1.getInt("Guest:");
String pno1=Integer.toString(num1);
t2.setText(pno1);
l1.setAdapter(new EfficientAdapter(TicketActivity.this));
}
private static class EfficientAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public EfficientAdapter(Context context){
mInflater=LayoutInflater.from(context);
// implementation of EfficientAdapter
}
public int getCount() {
return GlobalClass.myval.size()<GlobalClass.myvals.size()?GlobalClass.myval.size():GlobalClass.myvals.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position ;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String[] stockArr = new String[GlobalClass.myval.size()];
stockArr = GlobalClass.myval.toArray(stockArr);
String[] stockArr1=new String[GlobalClass.myvals.size()];
stockArr1=GlobalClass.myvals.toArray(stockArr1);
if(convertView==null){
convertView=mInflater.inflate(R.layout.list_ticket, null);
holder=new ViewHolder();
holder.Text1=(TextView) convertView.findViewById(R.id.textView1);
holder.Text2=(TextView) convertView.findViewById(R.id.textView2);
holder.Text3=(TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
holder.Text2.setText(stockArr[position]);
holder.Text3.setText(stockArr1[position]);
return convertView;
}
}
static class ViewHolder{
TextView Text1;
TextView Text2;
TextView Text3;
}
}