我正在尝试将TableRow
(从单独的布局中膨胀)添加到TableLayout
,但addView
是givint NullPointerException
。 TableLayout
本身位于TableRow
- 所以它是嵌套布局,TableLayout
内TableRow
。
这一切都发生在扩展 BaseAdapter 的自定义列表视图适配器中。
我在这一行收到NULLPointerException:trackEventstable.addView(tempEventRowForTrackEventsTable)
我尝试了很多不同的东西但不幸的是每次都会遇到相同的NPE错误。当我检查null
的膨胀布局时,它不会给出null。
(布局文件代码也在最后给出,您需要查看)
以下是getView
方法的完整代码:
public View getView(int arg0, View vi, ViewGroup arg2) {
// TODO Auto-generated method stub
ScheduleItemRecord y = scheduleItemsGeneralList.get(arg0);
vi = inflater.inflate(R.layout.schedule_list_session, null);
if(vi==null ){
Log.i("error", "null");
}
TextView sessionTitle = (TextView) vi.findViewById(R.id.sessionTitle);
int indexOfSession = y.getIndex();
SessionScheduleItem tempSession = sessionsRecordList.get(indexOfSession);
sessionTitle.setText(tempSession.getTitle());
TextView sessionSubtitle = (TextView) vi.findViewById(R.id.sessionSubtitle);
sessionSubtitle.setText(tempSession.getSubtitle() );
TrackScheduleItem[] tempSessionTracks = tempSession.getTracksInSession();
TableLayout table = (TableLayout)vi.findViewById(R.id.sessionTracksTable);
Log.i("GCA-A-Schedule", "Session Tracks Count: " + tempSessionTracks.length);
//add tracks to table
for(int i=0; i<tempSessionTracks.length; i++) {
Log.i("GCA-A-Schedule", "in outer Loop no: " + i);
TableRow tempRow = (TableRow) inflater.inflate(R.layout.session_track_table_row, null);
((TextView)tempRow.findViewById(R.id.session_track_name)).setText(tempSessionTracks[i].getTitle());
((TextView)tempRow.findViewById(R.id.session_track_chair)).setText("Chaired by: " + tempSessionTracks[i].getChair());
//here add events of respective track in another table
EventScheduleItem[] eventsInCurrentTrack = tempSessionTracks[i].getEventsInTrack();
TableLayout trackEventstable = (TableLayout)vi.findViewById(R.id.session_track_events_table);
Log.i("GCA-A-Schedule", "Track Events Count: " + eventsInCurrentTrack.length);
//From now PROBLEM AREA STARTS as I add another TableRow into the tracEventstable
//---------------------------------------------------------------
for(int j=0; j<eventsInCurrentTrack.length; j++) {
Log.i("GCA-A-Schedule", "in loop: " + j);
//adding each event into a this table
TableRow tempEventRowForTrackEventsTable = (TableRow) inflater.inflate(R.layout.track_events_table_row, null);
if(tempEventRowForTrackEventsTable == null){
Log.i("GCA-Schedule", "NULL SCENE");
}
((TextView)tempEventRowForTrackEventsTable.findViewById(R.id.track_event_start)).setText(eventsInCurrentTrack[j].getStart());
Log.i("GCA-A-Schedule", "Event title here: " + eventsInCurrentTrack[j].getTitle());
((TextView)tempEventRowForTrackEventsTable.findViewById(R.id.track_Event_end)).setText(eventsInCurrentTrack[j].getEnd());
((TextView)tempEventRowForTrackEventsTable.findViewById(R.id.track_event_title)).setText(eventsInCurrentTrack[j].getTitle());
((TextView)tempEventRowForTrackEventsTable.findViewById(R.id.track_event_location)).setText(eventsInCurrentTrack[j].getLocation());
//Adding the event row to Tracks
trackEventstable.addView(tempEventRowForTrackEventsTable);
^^^ Here i get NULL POINTER EXCEPTION
trackEventstable.requestLayout();
}
//adding the final track row
table.addView(tempRow);
}
table.requestLayout();
return vi;
}
过去几个小时我一直坚持这个问题。请帮助我。我将不胜感激,提前致谢
schedule_list_session.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="15dip"
android:background="#FCFFB7" >
<LinearLayout
android:id="@+id/schedule_session_header_block"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/yellow_background_pattern"
>
<!-- Title of Session -->
<TextView
android:id="@+id/sessionTitle"
android:contentDescription="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="8dp"
android:textColor="#0099CC"
android:text="123123123123123"
android:textSize="18sp" />
<!-- Title -->
<TextView
android:id="@+id/sessionSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="25dip"
android:layout_marginLeft="25dip"
android:textColor="#000000"
android:textSize="18sp"
android:paddingBottom="12dp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
<TableLayout
android:id="@+id/sessionTracksTable"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="15dip"
android:layout_below="@+id/schedule_session_header_block"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
session_track_table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp">
<RelativeLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#C4FFB7" >
<LinearLayout
android:id="@+id/schedule_tracks_header_block"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/green_background_pattern" >
<!-- Time -->
<TextView
android:id="@+id/session_track_name"
android:contentDescription="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="8dp"
android:textColor="#0099CC"
android:text="123123123123123"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Title -->
<TextView
android:id="@+id/session_track_chair"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/trackTitle"
android:paddingRight="25dip"
android:paddingBottom="8dp"
android:layout_marginLeft="25dip"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:fontFamily="sans-serif-light" />
</LinearLayout>
<TableLayout
android:id="@+id/session_track_events_table"
android:paddingLeft="35dip"
android:paddingTop="15dip"
android:layout_below="@+id/schedule_tracks_header_block"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_below="@+id/session_track_events_table"
android:background="#FCFFB7" />
track_events_table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/track_event_row_linearLayout_block"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout android:id="@+id/block_time_container"
android:orientation="vertical"
android:layout_width="72dp"
android:layout_height="match_parent"
android:background="@drawable/transparent_background_pattern"
android:padding="8dp">
<TextView android:id="@+id/track_event_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"
android:paddingTop="6sp" />
<TextView android:id="@+id/track_Event_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#3A2F0B"
android:paddingTop="4dp"/>
</LinearLayout>
<LinearLayout android:id="@+id/list_item_middle_container"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#B7F1FF"
android:clickable="false"
android:focusable="false"
android:padding="8dp"
android:orientation="vertical">
<TextView android:id="@+id/track_event_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:paddingTop="6sp"
android:textStyle="bold"
android:duplicateParentState="true" />
<TextView android:id="@+id/track_event_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:paddingBottom="8dp"
android:duplicateParentState="true" />
</LinearLayout>
答案 0 :(得分:0)
您正在从错误的xml布局参考中找到表格。
尝试替换此代码:
TableLayout trackEventstable = (TableLayout)vi.findViewById(R.id.session_track_events_table);
使用此代码:
TableLayout trackEventstable = (TableLayout)tempRow.findViewById(R.id.session_track_events_table);