自定义控件中的recyclerview和gridview空指针异常



我遵循本教程并应用一些更改,在recyclerview中获得了nullPointerException

我在gridview中也出现了这个错误

我初始化了recyclerview,这个代码怎么了?

custom_calendar_view.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"
android:background="@android:color/white">
<!-- date toolbar -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="30dp"
android:paddingRight="30dp">
<!-- prev button -->
</RelativeLayout>
<!-- days header -->
<LinearLayout
android:id="@+id/calendar_header"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
</LinearLayout>
<!-- days view -->
<android.support.v7.widget.RecyclerView
android:id="@+id/calendarRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

customCalendarView.java

public class customCalendarView extends LinearLayout {
// how many days to show, defaults to six weeks, 42 days
private static final int DAYS_COUNT = 42;
// current displayed month
private Calendar currentDate = Calendar.getInstance();
// internal components
private LinearLayout header;
private ImageView btnPrev;
private ImageView btnNext;
private TextView txtDate;
private RecyclerView grid;
public customCalendarView(Context context) {
super(context);
initControl(context);
}
public customCalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public customCalendarView(Context context, AttributeSet attrs, int     defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initControl(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_calendar_view, this);
//assignClickHandlers();
updateCalendar();
}
private void assignUiElements() {
// layout is inflated, assign local variables to components
header = (LinearLayout) findViewById(R.id.calendar_header);
btnPrev = (ImageView) findViewById(R.id.calendar_prev_button);
btnNext = (ImageView) findViewById(R.id.calendar_next_button);
txtDate = (TextView) findViewById(R.id.calendar_date_display);
grid = (RecyclerView) findViewById(R.id.calendarRecycler);
}
public void updateCalendar() {
assignUiElements();
List<calendarInf> cells = new ArrayList<>();
Calendar calendar = (Calendar) currentDate.clone();
// determine the cell for current month's beginning
calendar.set(Calendar.DAY_OF_MONTH, 1);
int monthBeginningCell = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// move calendar backwards to the beginning of the week
calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell);
// fill cells
while (cells.size() < DAYS_COUNT) {
calendarInf ci = new calendarInf();
ci.date1 = calendar.getTime().toString();
ci.date2 = "2";
ci.date3 = "3";
cells.add(ci);
//Log.d("cells", "updateCalendar: "+cells.);
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
CustomCalendarAdapter adapter = new   CustomCalendarAdapter(getContext(), cells);
RecyclerView.LayoutManager mLayoutManager=new GridLayoutManager(getContext(),2);
grid.setLayoutManager(mLayoutManager);
grid.setAdapter(adapter);
}
}

CostumCalendarAdapter.java

public class CustomCalendarAdapter extends   RecyclerView.Adapter<CustomCalendarAdapter.calendarViewHolder >{
private LayoutInflater inflater;
List<calendarInf> data = Collections.emptyList();
Context context;
public CustomCalendarAdapter(Context context,List<calendarInf> data)
{
inflater = LayoutInflater.from(context);
this.data = data;
this.context=context;
}

@Override
public CustomCalendarAdapter.calendarViewHolder         onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.calendar_grid,parent,false);
calendarViewHolder holder=new calendarViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(CustomCalendarAdapter.calendarViewHolder   holder, int position) {
calendarInf current=data.get(position);
holder.txtDate1.setText(current.date1);
holder.txtDate2.setText(current.date2);
holder.txtDate3.setText(current.date3);
}
@Override
public int getItemCount() {
return data.size();
}
class calendarViewHolder extends RecyclerView.ViewHolder{
TextView txtDate1;
TextView txtDate2;
TextView txtDate3;
public calendarViewHolder(View itemView) {
super(itemView);
txtDate1= (TextView) itemView.findViewById(R.id.txtDate1);
txtDate2= (TextView) itemView.findViewById(R.id.txtDate2);
txtDate3= (TextView) itemView.findViewById(R.id.txtDate3);

}
}
}

CalendarActivity.java

public class CalendarActivity extends BaseActivity {
//FragmentManager manager;
private BottomSheetBehavior mBottomSheetBehavior;
TextView txtHello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);


View bottomSheetView = findViewById(R.id.bottom_sheet);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

customCalendarView cv = ((customCalendarView)findViewById(R.id.calendar_view));
cv.updateCalendar();

}
}

activity_calendar.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.aysha.todocln.CalendarActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/activity_calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.aysha.todocln.customCalendarView
android:id="@+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<!-- bottom sheet layout -->
<RelativeLayout
android:id="@+id/bottom_sheet"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="320dp"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_peekHeight="70dp">
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

错误日志类别:

12-03 03:56:55.184 6750-6750/com.example.aysha.todoclnE/AndroidRuntime:致命异常:main流程:com.example.aysha.todocln,PID:6750java.lang.RuntimeException:无法启动活动组件信息{com.example.aysha.todocn/com.example.aysha.todocn.CalendarActivity}:java.lang.NullPointerException:尝试调用虚拟方法"void"android.support.v7.widget.CyclerView.setLayoutManager(android.ssupport.v7.widget.RyclerView$LayoutManager)'关于null对象引用在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2325)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2387)在android.app.ActivityThread.access上$800(ActivityThreads.java:151)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1303)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5254)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:698)由以下原因引起:java.lang.NullPointerException:尝试调用虚拟方法"void"android.support.v7.widget.CyclerView.setLayoutManager(android.ssupport.v7.widget.RyclerView$LayoutManager)'关于null对象引用在com.example.aysa.todocln.customCalendarView.updateCalendar(customCalendarView.java:103)在com.example.aysa.todocln.CalendarActivity.onCreate(CalendarActivity.java:87)在android.app.Activity.performCreate(Activity.java:5990)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2278)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2387)在android.app.ActivityThread.access上$800(ActivityThreads.java:151)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1303)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5254)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:698)

以这种方式更改代码

LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view =  inflater.inflate(R.layout.custom_calendar_view, null);
// add Initialization Here
header = (LinearLayout)view.findViewById(R.id.calendar_header);
btnPrev = (ImageView)view.findViewById(R.id.calendar_prev_button);
btnNext = (ImageView)view.findViewById(R.id.calendar_next_button);
txtDate = (TextView)view.findViewById(R.id.calendar_date_display);
grid = (RecyclerView)view.findViewById(R.id.calendarRecycler);

tnx感谢您的帮助铁人我也修第二部分只需将代码更改为

充气器=充气器=(布局充气器)getContext().getSystemService(Context.LAYOUT_inflater_SERVIC‌​E) ;视图视图=充气机.充气(R.layout.custom_calendar_View,this);

使用这个而不是null

最新更新