2016-12-03 4 views
1

私はthis tutorialに従い、変更を加えてnullPointerExceptionrecyclerviewにしました。カスタムコントロールのrecyclerviewとgridview nullポインタ例外

私はこのコードについて間違って何recyclerviewを初期化gridviewすぎ

でこのエラーを得ましたか。

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> 

エラーLogCat:

12-03 03: 56:55.184 6750-6750/com.example.aysha.todocln E/AndroidRuntime:致命的除外:メイン プロセス:com.example.aysha.todocln、PID:6750 java.lang.RuntimeException:アクティビティを開始できません ComponentInfo {com.example.aysha.todocln/com.example.aysha.todocln.CalendarActivity}: java.lang.NullPointerException:仮想メソッドを呼び出そうとしました 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support .v7.widget.RecyclerView $のLayoutManager「)nullオブジェクト参照 上 でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) android.app.Acでアンドロイドで android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) でandroid.app.ActivityThread.access $ 800(ActivityThread.java:151) でtivityThread.handleLaunchActivity(ActivityThread.java:2387) 。 os.Handler.dispatchMessage(Handler.java:102) とandroid.os.Looper.loop(Looper.java:135) とandroid.app.ActivityThread.main(ActivityThread.java:5254) at java.lang。メソッド.invoke(ネイティブメソッド) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(Zygote Init.java:903) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 原因:java.lang.NullPointerException:仮想メソッド を呼び出そうとしました 'void android.support.v7ウィジェット。 com.exampleで com.example.aysha.todocln.customCalendarView.updateCalendar(customCalendarView.java:103)でnullオブジェクト参照 上RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $のLayoutManager「) .aysha.todocln.CalendarActivity.onCreate android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)で(CalendarActivity.java:87)android.app.Activity.performCreate(Activity.java:5990)で で android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.access $ 800からandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) android.app.ActivityThread $ H.handleMessageで(ActivityThread.java:151) (ActivityThread.java:1303) android.app.ActivityThread.mainでandroid.os.Looper.loop(Looper.java:135) でandroid.os.Handler.dispatchMessage(Handler.java:102) (ActivityThread.java:5254) ででjava.lang.reflect.Method.invoke(ネイティブメソッド) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit com.android.internal.os.ZygoteInit.mainで$ MethodAndArgsCaller.run(ZygoteInit.java:903) (ZygoteInit.java:698)

答えて

0
はあなたのコードをこのように変更し

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); 
+0

@ayshamanあなたの問題を解決しましたか? – Ironman

+0

あなたに注目してください。しかし、私はまだその方法を持っていない。私は今すぐクリックを処理したくありません。どのように私はそのメソッドをコピーし、その行のコメントを外して何もしていない –

+0

私はrecycUpdate()の中でrecyclerViewを初期化し、それをupdateCalendar()で呼び出す –

0

TNXあなたの助けのアイアンマン のためにたくさん私はあまりにも だけ

インフレータ=インフレータ=(LayoutInflater)のgetContext()getSystemService(Context.LAYOUT_INFLATER_SERVIC E)にコードを変更する第二部を固定します。ビュービュー= inflater.inflate(R.layout.custom_calendar_view、this);

代わりにこの使用する