2015-09-29 2 views
1

соединения кода составного XML вид: -findViewById (R.id.view_id) возвращает нуль в случае зрения

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/dayRelativeLayout" 
    android:layout_width="30dip" 
    android:layout_height="45dip" 
    android:background="@drawable/selectable_day_layout_border"> 

    <TextView android:id="@+id/eventTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:textSize="12dip" 
     android:visibility="invisible" 
     android:layout_margin="2.1dip" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 

    <TextView 
     android:id="@+id/dayTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_gravity="center" 
     android:layout_below="@+id/eventTextView" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 
</RelativeLayout> 

Ниже приведен Java код выше контрольного соединения: -

public class CalendarGridView extends RelativeLayout 
{ 
    private Context context = null; 

    private TextView eventTextView = null; 

    private TextView dayTextView = null; 

    public CalendarGridView(Context context) 
    { 
     super(context); 

     this.context = context; 

     init(); 
    } 

    public CalendarGridView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     this.context = context; 
     init(); 
    } 

    public CalendarGridView(Context context, AttributeSet attrs,int defStyle) 
    { 
     super(context, attrs, defStyle); 
     this.context = context; 
     init(); 
    } 

    private void init() 
    { 
     int id = (int)Math.abs(Math.random()*100000); 

     super.setId(id); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     addView(inflater.inflate(R.layout.day_view, null)); 

     eventTextView = (TextView) findViewById(R.id.eventTextView); 

     dayTextView = (TextView) findViewById(R.id.dayTextView); 

     eventTextView.setOnClickListener(new GridClickListener()); 

     dayTextView.setOnClickListener(new GridClickListener()); 
    } 

    public TextView getEventTextView() 
    { 
     return eventTextView; 
    } 

    public TextView getDayTextView() 
    { 
     return dayTextView; 
    } 

    class GridClickListener implements OnClickListener 
    { 
     @Override 
     public void onClick(View v) 
     { 
      calculateClickedGrid(v); 
     } 
    } 

    private void calculateClickedGrid(View v) 
    { 
     int id = ((ViewGroup)v.getParent()).getId(); 

     GridCordinates cordinates = GridViewUtil.getGridCordinate(id); 

     if(cordinates != null) 
     { 

     } 
    } 
} 

Ниже приведен код GridLayout XML, в котором я использую выше контрольного соединения: -

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:rowCount="8" 
    android:columnCount="7" 
    android:background="@drawable/selectable_day_layout_border"> 

    <!-- Row 0--> 
    <ImageButton 
     android:id="@+id/previousMonthImageButton" 
     android:layout_row="0" 
     android:layout_columnSpan="1" 
     android:layout_column="0" 
     android:layout_gravity="center" 
     android:layout_margin="3dip" 
     android:padding="3dip" 
     android:background="#7F7CD9" 
     android:src="@drawable/navigation_previous_item"/> 
    <TextView 
     android:id="@+id/monthNameTextView" 
     android:text="September" 
     android:layout_row="0" 
     android:layout_columnSpan="5" 
     android:layout_column="1" 
     android:layout_gravity="center" 
     android:layout_margin="3dip" 
     android:padding="3dip" 
     android:textSize="25dip" 
     android:background="#7F7CD9"/> 

    <ImageButton 
     android:id="@+id/nextMonthImageButton" 
     android:layout_row="0" 
     android:layout_columnSpan="1" 
     android:layout_column="6" 
     android:layout_gravity="center" 
     android:layout_margin="3dip" 
     android:padding="3dip" 
     android:background="#7F7CD9" 
     android:src="@drawable/navigation_next_item"/> 

    <!-- Row 1--> 

    <com.gp.app.minote.calendar.ui.CalendarGridView 
     android:id="@+id/day1" 
     android:layout_row="2" 
     android:layout_column="0" 
     android:layout_width="30dip" 
     android:layout_height="40dip" 
     android:layout_margin="2dip" 
     android:padding="3dip" 
     android:layout_gravity="center" 
     android:background="@drawable/selectable_day_layout_border"/> 

    <com.gp.app.minote.calendar.ui.CalendarGridView 
     android:layout_width="30dip" 
     android:layout_height="40dip" 
     android:id="@+id/day2" 
     android:layout_row="2" 
     android:layout_column="1" 
     android:layout_margin="2dip" 
     android:padding="3dip" 
     android:layout_gravity="center" 
     android:background="@drawable/selectable_day_layout_border"/> 

    <com.gp.app.minote.calendar.ui.CalendarGridView 
     android:layout_width="30dip" 
     android:layout_height="40dip" 
     android:id="@+id/day3" 
     android:layout_row="2" 
     android:layout_column="2" 
     android:layout_margin="2dip" 
     android:layout_gravity="center" 
     android:padding="3dip" 
     android:background="@drawable/selectable_day_layout_border"/> 

<!-- More CalendarGridViews but deleted for clarity --> 
</GridLayout> 

Ниже упомянуты это код из CLA сс, которая простирается GridLayout и использует CalendarGridViews: -

public class ProfessionalPACalendarView extends GridLayout 
{ 
    private Context context; 
    private Calendar cal; 
    private ImageButton previousMonthImageButton = null; 
    private ImageButton nextMonthImageButton = null; 
    private TextView monthNameTextView = null; 
    private List<DateInformation> dateList = new ArrayList<>(); 
    private List<CalendarGridView> calendarGrids = new ArrayList<>(); 
    private RelativeLayout base; 
    private ImageView next,prev; 
    private boolean isWithoutEvents = false; 
    private int id = 0; 

    public ProfessionalPACalendarView(Context context) 
    { 
     super(context); 
     this.context = context; 
     cal= Calendar.getInstance(); 

     if(!isWithoutEvents) 
     { 
      CalendarDBManager.getInstance().addDataChangeListener(this); 
     } 

     initView(); 
     this.isWithoutEvents = isWithoutEvents; 
    } 

    public ProfessionalPACalendarView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
     initView(); 
    } 

    public ProfessionalPACalendarView(Context context, AttributeSet attrs,int defStyle) 
    { 
     super(context, attrs, defStyle); 
     this.context = context; 
     initView(); 
    } 

    private void initView() 
    { 
     init(); 

     Calendar calendar = Calendar.getInstance(); 
     refreshDays(calendar.get(Calendar.DATE), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR)); 
     fillCalendarViewWithDates(); 
    } 

    private void init() 
    { 
     int id = (int)Math.abs(Math.random() * 100000); 

     super.setId(id); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View view = inflater.inflate(R.layout.calendar_grid_layout, null); 

     addView(view); 

     previousMonthImageButton = (ImageButton) findViewById(R.id.previousMonthImageButton); 

     previousMonthImageButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       previousMonth(); 
      } 
     }); 

     monthNameTextView = (TextView) findViewById(R.id.monthNameTextView); 

     nextMonthImageButton = (ImageButton) findViewById(R.id.nextMonthImageButton); 

     nextMonthImageButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       nextMonth(); 
      } 
     }); 

     calendarGrids.add((CalendarGridView) findViewById(R.id.day2)); 
     calendarGrids.add((CalendarGridView) findViewById(R.id.day3)); 
} 

Проблема: - findViewById (ID) в строках ниже: -

previousMonthImageButton =(ImageButton)findViewById(R.id.previousMonthImageButton); 

monthNameTextView = (TextView) findViewById(R.id.monthNameTextView); 

nextMonthImageButton = (ImageButton) findViewById(R.id.nextMonthImageButton); 

возвращает не нулевое значение, тогда как findViewById (ID) в строках ниже: -

calendarGrids.add((CalendarGridView) findViewById(R.id.day2)); 
calendarGrids.add((CalendarGridView) findViewById(R.id.day3)); 

возвращение null значение.

Я пытаюсь решить эту проблему долго, но не получаю разницы, почему тот же метод возвращает значение null и не пустое значение для разных представлений. Я думаю, что это имеет отношение к составным представлениям. Пожалуйста помоги.

ответ

2

Вы инициализировать View view = inflater.inflate(R.layout.calendar_grid_layout, null);

Передайте View Object.

 previousMonthImageButton =(ImageButton)view.findViewById(R.id.previousMonthImageButton); 
     monthNameTextView = (TextView)view. findViewById(R.id.monthNameTextView); 
     nextMonthImageButton = (ImageButton)view.findViewById(R.id.nextMonthImageButton); 

Попробуйте этот путь. Надеюсь, это вам поможет.

+0

Я уже пробовал это, но он не работает в моем случае. –

Смежные вопросы