gpt4 book ai didi

java - findViewById(R.id.view_id) 在复合 View 的情况下返回 null

转载 作者:行者123 更新时间:2023-11-29 20:23:31 25 4
gpt4 key购买 nike

复合 View xml代码:-

<?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>

下面提到的是扩展 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));

返回空值。

我已经尝试了很长时间来解决这个问题,但没有明白为什么相同的方法会为不同的 View 返回空值和非空值。我认为这与复合 View 有关。请帮忙。

最佳答案

你初始化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);

试试这个方法,希望对你有帮助。

关于java - findViewById(R.id.view_id) 在复合 View 的情况下返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com