我正在尝试使用包含ViewPager
的自定义视图来填充自定义对话框。我收到类似“找不到ViewPager
内片段的视图”的错误。
ViewPager
似乎在这里引起了一些问题,我找不到这种确切情况的答案。
尝试使用alertdialog.builder
来构建对话框,但错误仍然存在。试图获取正确的上下文仍然无法解决错误。
可以在此处找到回购链接-https://github.com/Alfurquan/CustomCalendarView
这是我的主要活动的onCreate
setContentView(R.layout.activity_month_picker);
btn = findViewById(R.id.clickToOpn);
customMonthPicker = findViewById(R.id.customMonthPicker1);
// customMonthPicker.setOnMonthSelectedListener(new OnMonthSelectedListener() {
// @Override
// public void onSelectedMonth(int month) {
// Log.d("msgMonth", String.valueOf(month));
// }
// });
//
// customMonthPicker.setOnYearSelectedListener(new OnYearSelectedListener() {
// @Override
// public void onYearSelected(int year) {
// Log.d("msgYear", String.valueOf(year));
// }
// });
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomMonthAndYearPickerDialog dialog = new CustomMonthAndYearPickerDialog(MonthPickerActivity.this);
dialog.setOnMonthSelectedListener(new OnMonthSelectedListener() {
@Override
public void onSelectedMonth(int month) {
Log.d("msgMonth", String.valueOf(month));
}
});
dialog.setOnYearSelectedListener(new OnYearSelectedListener() {
@Override
public void onYearSelected(int year) {
Log.d("msgYear", String.valueOf(year));
}
});
dialog.show();
}
});
这是自定义对话框代码
public CustomMonthAndYearPickerDialog(@NonNull Context context) {
super(context);
setContentView(R.layout.custom_month_dialog);
mContext = context;
initialize();
setClickListeners();
}
public CustomMonthAndYearPickerDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected CustomMonthAndYearPickerDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
private void initialize() {
customMonthPicker = findViewById(R.id.customMonthPicker1);
btnDone = findViewById(R.id.btnDone1);
btnCancel = findViewById(R.id.btnCancel1);
}
这是自定义视图代码
Activity activity = getActivityFromContext(getContext());
yearPagerAdapter = new YearPagerAdapter(((AppCompatActivity)activity).getSupportFragmentManager(),context,fragList);
viewPager.setAdapter(yearPagerAdapter);
@Nullable
public Activity getActivityFromContext(@NonNull Context context){
while (context instanceof ContextWrapper) {
if (context instanceof Activity) return (Activity) context;
context = ((ContextWrapper)context).getBaseContext();
}
return null; //we failed miserably
}
这是viewpager适配器
public class YearPagerAdapter extends FragmentPagerAdapter {
private Context context;
YearFragment[] fragList;
private CalendarManager calendarManager;
public YearPagerAdapter(FragmentManager fm,Context context,YearFragment[] fragList) {
super(fm);
this.context = context;
this.fragList = fragList;
calendarManager = new CalendarManager(context);
}
@Override
public Fragment getItem(int position) {
return fragList[position];
}
@Override
public int getCount() {
return 3;
}
public void setYears(Calendar currentYear){
Calendar prevYear = Calendar.getInstance();
prevYear.setTime(currentYear.getTime());
prevYear.add(Calendar.MONTH,-12);
int prev = prevYear.get(Calendar.YEAR);
Calendar nextYear = Calendar.getInstance();
nextYear.setTime(currentYear.getTime());
nextYear.add(Calendar.MONTH, 12);
int next = nextYear.get(Calendar.YEAR);
int cur = currentYear.get(Calendar.YEAR);
ArrayList<Integer> prevYears,currentYears,nextYears;
prevYears = calendarManager.getYearList(prevYear);
nextYears = calendarManager.getYearList(nextYear);
currentYears = calendarManager.getYearList(currentYear);
fragList[0].updateUI(prevYears);
fragList[1].updateUI(currentYears);
fragList[2].updateUI(nextYears);
}
}
这是片段
package com.example.customcalendar.fragments;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import com.example.customcalendar.R;
import com.example.customcalendar.adapter.YearGridAdapter;
import com.example.customcalendar.interfaces.OnYearSelectedListener;
import java.util.ArrayList;
import java.util.Calendar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class YearFragment extends Fragment {
private ArrayList<Integer> yearList;
private View view;
private GridView yearGrid;
int currentYear;
public static int selectedYear;
private YearGridAdapter yearGridAdapter;
public static OnYearSelectedListener onYearSelectedListener;
public YearFragment() {
}
@SuppressLint("ValidFragment")
public YearFragment(ArrayList<Integer> yearList) {
this.yearList = yearList;
}
public static YearFragment newInstance(ArrayList<Integer> yearList){
return new YearFragment(yearList);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.year_view,container,false);
initializeVariables();
setUpYearGridAdapter();
setUpYearGridClicks();
return view;
}
private void initializeVariables() {
yearGrid = view.findViewById(R.id.yearGrid);
Calendar calendar = Calendar.getInstance();
currentYear = calendar.get(Calendar.YEAR);
selectedYear = currentYear;
}
private void setUpYearGridAdapter() {
yearGridAdapter = new YearGridAdapter(view.getContext(),yearList,currentYear,selectedYear);
yearGrid.setAdapter(yearGridAdapter);
}
private void setUpYearGridClicks() {
yearGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
int year = yearGridAdapter.getItem(position);
selectedYear = year;
onYearSelectedListener.onYearSelected(selectedYear);
setUpYearGridAdapter();
}
});
}
public void updateUI(ArrayList<Integer> yearList){
this.yearList = yearList;
setUpYearGridAdapter();
}
}
这是对话框的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/header"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100sp">
<TextView
android:id="@+id/monthName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="April"
android:padding="10sp"
android:textSize="24sp"
android:textColor="#000" />
<TextView
android:id="@+id/yearName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2019"
android:padding="10sp"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#000" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2sp"
android:background="@color/grey"/>
</LinearLayout>
<com.example.customcalendar.views.CustomMonthPicker
android:id="@+id/currentMonthPicker"
android:layout_width="match_parent"
android:layout_height="200sp"/>
<LinearLayout
android:orientation="horizontal"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnCancel1"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="0.5"/>
<Button
android:id="@+id/btnDone1"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:text="Done"
android:layout_weight="0.5"/>
</LinearLayout>
</LinearLayout>
这是我收到的错误消息:
Error message - "Process: com.example.customcalendar, PID: 17596
java.lang.IllegalArgumentException: No view found for id 0x7f0700ab (com.example.customcalendar:id/yearPager) for fragment YearFragment{20a1ebd5 #0 id=0x7f0700ab android:switcher:2131165355:1}"