我有一个附加到活动的对话框片段。事情看起来很清楚,直到出现这个奇怪的错误。
当我初始化数组时,对话框构建器可以正常工作"哟"像
String [] yo = {" a"," b"," c"," d"," e& #34;};
但是,在使用其他类中的静态字符串2-D数组初始化数组时,logcat会显示下面附带的错误。我已经手动检查(通过打印到logcat)如果字符串yo已经初始化并且确实有。有人能帮帮我吗?``
String[] yo=appstartreceiver.today[2];
Log.d("Update update",String.valueOf(yo.length));
boolean[] happened=new boolean[5];
boolean[] missed=new boolean[5];
AlertDialog.Builder build=new AlertDialog.Builder(getActivity());
build.setTitle("Select classes that happened and classes that were missed");
build.setMultiChoiceItems(yo,happened,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Log.d("yo","Sahi ho");
10-27 17:57:09.249 4869-4869/com.example.shakar.klassjum E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.shakar.klassjum, PID: 4869
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at com.android.internal.app.AlertController$AlertParams$1.getView(AlertController.java:879)
at android.widget.AbsListView.obtainView(AbsListView.java:2240)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1088)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
}
});
答案 0 :(得分:0)
刚刚找到了解决方案。似乎数组“yo”在后面的索引中有一些空值。这就是抛出空指针异常的原因。所以我做的是
List<String> list = new ArrayList<String>(Arrays.asList(y));
list.removeAll(Collections.singleton(null));
String[]yo=list.toArray(new String[list.size()]);
对话框构建器完美运行:D