android studio在我声明CalendarView时崩溃

zd287kbt  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(378)

一个非常简单的有片段的应用程序,我有一个活动,其中包含一个从侧边导航抽屉打开的日历视图。当您打开“logs”片段时,它会将您发送到calendaractivity,以便您选择要记录信息的日期。但是,当您这样做时,应用程序会崩溃,我发现如果您删除行“logcalendar=(calendarview)findviewbyid(r.id.logcalendar);”它不会崩溃,但很明显我现在不能用日历来确定日期。
有没有人知道为什么会崩溃,或者有没有其他解决方案能为我想要实现的目标提供相同的解决方案。
谢谢。
日历活动类

public class CalendarActivity extends AppCompatActivity {

    private static final String TAG = "CalendarActivity";

    private CalendarView logCalendar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        logCalendar = (CalendarView) findViewById(R.id.logCalendar);

        logCalendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                String date =  dayOfMonth + "/" + month +"/" + year;
                Log.d(TAG, "onSelectedDayChange: date:" + date);
            }
        });

    }
}

打开calenderactivity类的类

private TextView currentDate;
    private Button dateButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_logs, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        currentDate = (TextView) view.findViewById(R.id.currentDate);
        dateButton = (Button) view.findViewById(R.id.dateButton);

        Intent intent = new Intent(getActivity(), CalendarActivity.class);
        startActivity(intent);

        super.onViewCreated(view, savedInstanceState);
    }
}

我的logcat

2020-12-20 16:45:53.987 28316-28316/com.example.co3201weighttrainingapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.co3201weighttrainingapplication, PID: 28316
    java.lang.ClassCastException: com.applandeo.materialcalendarview.CalendarView cannot be cast to android.widget.CalendarView
        at com.example.co3201weighttrainingapplication.LogsFragment.onViewCreated(LogsFragment.java:48)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
watbbzwu

watbbzwu1#

您发布的logcat解释了错误本身。请看这一行: java.lang.ClassCastException: com.applandeo.materialcalendarview.CalendarView cannot be cast to android.widget.CalendarView .
看来你已经进口了 android 本地的 calendarView 而不是你的 material calendar view . 检查您的 imports 在你的 CalendarActivity class 你可能会发现这样的东西: import android.widget.CalendarView; 把它换成 import com.applandeo.materialcalendarview.CalendarView; . 我猜你在用 applandeo 此网站的材质日历视图。

相关问题