我的LinearLayout有这个onclicklistener:
LinearLayout closedbets = (LinearLayout) findViewById (R.id.closedbetslayout);
closedbets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView closedtxt = (TextView) findViewById(R.id.settledbetstxtview);
closedtxt.setTextColor(Color.parseColor("#09AD21"));
TextView openbetstxt = (TextView) findViewById(R.id.openbetstxtview);
openbetstxt.setTextColor(Color.parseColor("#B4B5AE"));
listwriter = getListwriter();
if (listwriter.size() ==0) {
Log.d("EMPTY LISTWRITER","empty");
TextView emptybet = (TextView) findViewById(R.id.nobetstxtbox);
emptybet.setText("None of your bets have been settled yet.");
emptybet.setVisibility(View.VISIBLE);
}
else {
PopulateList();
}
}
});
问题是,即使列表编写器为空并且执行了日志,由于某种原因,PopulateList();
方法也会被执行,我在适配器中得到nullpointerexception
,用于填充列表因为汇编程序是空的。
logcat的
06-29 18:51:08.492 28394-28394/com.example.albert.bettertipapp D/EMPTY LISTWRITER﹕ empty
06-29 18:51:08.512 28394-28394/com.example.albert.bettertipapp D/AndroidRuntime﹕ Shutting down VM
06-29 18:51:08.512 28394-28394/com.example.albert.bettertipapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4172cda0)
06-29 18:51:08.532 28394-28394/com.example.albert.bettertipapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.albert.bettertipapp, PID: 28394
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.albert.betterapp.DisplayAllBets$MyListAdapter.getView(DisplayAllBets.java:764)
at android.widget.AbsListView.obtainView(AbsListView.java:2733)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
at android.widget.ListView.onMeasure(ListView.java:1186)
at android.view.View.measure(View.java:17396)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17396)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:17396)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17396)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17396)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
at android.view.View.measure(View.java:17396)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)