重建项目时出现错误:不兼容的类型:ModalEvent无法转换为Context

时间:2018-11-07 05:58:57

标签: android

但是,当我对this appears进行编码时,请单击链接以查看图像。.btw这是一个inflateLayout,当我单击modal_event.xml中的按钮(btnDateTime)时,它将显示DatePickerDialog。 ModalEvent.java不是我的主要活动。仅当我单击我的NavigationView中的项目时,才会显示ModalEvent.java。请帮助我需要所有人:) 这段代码在我的ModalEvent.java

package com.example.khalessi_jb.reminderapplication;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;


public class ModalEvent extends BottomSheetDialogFragment {

TextView txtDateandTime;
Button btnsDateTime;

Calendar calendar;
DatePickerDialog datePickerDialog;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    btnsDateTime = (Button) getView().findViewById(R.id.btnDateTime);
    txtDateandTime =(TextView)getView().findViewById(R.id.txtDateTime);

    View v = inflater.inflate(R.layout.modal_event,container,false);

    return v;


    btnsDateTime.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
           calendar = Calendar.getInstance();
            int daysss = calendar.get(Calendar.DAY_OF_MONTH);
            int monthss =calendar.get(Calendar.MONTH);
            int yearss =calendar.get(Calendar.YEAR);

            datePickerDialog = new DatePickerDialog(ModalEvent.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int mYear, int mMonth, int mDay) {
                    txtDateandTime.setText(mDay +"/" + (mMonth +1)+"/"+mYear);
                }
            },yearss,monthss,daysss);
            datePickerDialog.show();
        }
    });


}

}

这是我的modal_event.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">


<TextView
android:id="@+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Event" />

<EditText
android:id="@+id/txtEventName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView12"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Event Title"
android:inputType="textLongMessage" />

<EditText
android:id="@+id/txtWhere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView15"
android:layout_alignStart="@+id/textView13"
android:ems="10"
android:hint="Event Location"
android:inputType="textLongMessage" />

<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtEventName"
android:layout_alignStart="@+id/txtEventName"
android:layout_marginTop="-1dp"
android:text="When" />

<TextView
android:id="@+id/txtDateTime"
android:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView13"
android:layout_alignStart="@+id/textView12"
android:layout_marginStart="182dp"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="Date/Time"
android:inputType="time"
android:textSize="15dp" />

<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView10"
android:layout_alignStart="@+id/textView13"
android:layout_marginStart="4dp"
android:text="Where" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView12"
android:layout_alignParentEnd="true"
android:layout_marginTop="216dp"
android:layout_marginEnd="132dp"
android:text="Create" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_alignParentEnd="true"
android:layout_marginTop="-1dp"
android:layout_marginEnd="15dp"
android:text="Close" />

<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtDateTime"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="21dp"
android:layout_toStartOf="@+id/txtDateTime"
android:text="at" />

<TextView
android:id="@+id/textView14"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/button"
android:layout_alignParentTop="true"
android:layout_marginStart="96dp"
android:layout_marginTop="378dp" />

<Button
android:id="@+id/btnDateTime"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView13"
android:layout_alignParentStart="true"
android:layout_marginStart="-4dp"
android:text="Choose Date/Time" />

</RelativeLayout>

当我单击按钮时,这将显示在日志猫上

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    at com.example.khalessi_jb.reminderapplication.ModalEvent.onCreateView(ModalEvent.java:30)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6259)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

1 个答案:

答案 0 :(得分:0)

您需要传递 getContext() getActivity()而不是 ModalEvent.this 作为DatePickerDialog的第一个参数。您的代码如下所示:

datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int mYear, int mMonth, int mDay) {
                txtDateandTime.setText(mDay +"/" + (mMonth +1)+"/"+mYear);
            }
        },yearss,monthss,daysss);

尝试并让我知道

似乎该部分起作用了,现在您只需要在onCreateView方法中进行一些重构:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.modal_event,container,false);
    btnsDateTime = (Button) v.findViewById(R.id.btnDateTime);
    txtDateandTime =(TextView)v.findViewById(R.id.txtDateTime);

    btnsDateTime.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
       calendar = Calendar.getInstance();
        int daysss = calendar.get(Calendar.DAY_OF_MONTH);
        int monthss =calendar.get(Calendar.MONTH);
        int yearss =calendar.get(Calendar.YEAR);

        datePickerDialog = new DatePickerDialog(ModalEvent.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int mYear, int mMonth, int mDay) {
                txtDateandTime.setText(mDay +"/" + (mMonth +1)+"/"+mYear);
            }
        },yearss,monthss,daysss);
        datePickerDialog.show();
    }
    });

   return v;
}