我正在尝试迭代值的映射并将预定义布局的文本设置为地图的值 我在地图上第一次迭代后得到一个NullPointerException,我不知道为什么。
对此的任何帮助将不胜感激。
片段
Set keys = pairedValues.keySet();
View summaryCell = inflater.inflate(R.layout.summary_list_cell, null);
cellTitle = (TextView) view.findViewById(R.id.summaryListItemTitle);
cellText = (TextView) view.findViewById(R.id.summaryListItemText);
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
switch(key) {
case "eventCode": cellTitle.setText("Event Code");
cellText.setText(pairedValues.get(key).second);
break;
case "accountId": cellTitle.setText(pairedValues.get(key).first);
cellText.setText(pairedValues.get(key).second);
break;
}
linearLayout.addView(summaryCell);
}
布局 这个片段位于RelativeLayout内部,其中包含大量其他内容。
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/summaryScrollView"
android:layout_below="@+id/summaryFragback"
android:layout_above="@+id/summarySaveEventButton">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/summaryFragback"
android:id="@+id/summaryVerticalView"
android:padding="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/summarySaveEventButton">
</LinearLayout>
</ScrollView>
将预定义的布局添加到LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:textColor="@color/Black"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:id="@+id/summaryListItemTitle"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@color/White"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Text"
android:textColor="@color/Dark_Background"
android:id="@+id/summaryListItemText"
android:layout_below="@+id/summaryListItemTitle"
android:layout_alignLeft="@+id/summaryListItemTitle"
android:layout_alignStart="@+id/summaryListItemTitle" />
(最后但并非最不重要)LogCat
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at com.koodoocreative.isnapshot.fragments.SummaryFragment.onCreateView(SummaryFragment.java:79)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5257)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-17 18:33:47.571 6527-6527/com.koodoocreative.isnapshot E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:4)
既然你得到了你的视图:
View summaryCell = inflater.inflate(R.layout.summary_list_cell, null);
您必须使用那个(summaryCell
)来检索其子女:
cellTitle = (TextView) summaryCell.findViewById(R.id.summaryListItemTitle);
cellText = (TextView) summaryCell.findViewById(R.id.summaryListItemText);