Android 当我声明我的 CalenderView 时 Studio 崩溃
Android Studio crash when I declare my CalenderView
一个非常简单的包含片段的应用程序,我有一个 activity 包含一个从侧面导航抽屉打开的 CalendarView。当您打开“日志”片段时,它会将您发送到 CalendarActivity 供您选择要记录信息的日期。但是,当您执行此操作时应用程序崩溃,我发现您是否删除了行“logCalendar = (CalendarView) findViewById(R.id.logCalendar);”它不会崩溃,但显然我现在不能使用日历来获取日期。
有谁知道它崩溃的原因,或者任何其他解决方案可以为我想要实现的目标提供相同的解决方案。
谢谢。
日历活动class
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);
}
});
}
}
Class 打开 CalenderActivity class
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.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)
您发布的 logcat 解释了错误本身。看到这一行:-
java.lang.ClassCastException: com.applandeo.materialcalendarview.CalendarView cannot be cast to android.widget.CalendarView
.
您似乎导入了 android
本机 calendarView
而不是 material calendar view
。检查 CalendarActivity class
中的 imports
,您可能会发现类似这样的内容:- import android.widget.CalendarView;
删除它并将其更改为 import com.applandeo.materialcalendarview.CalendarView;
。我假设您正在使用 applandeo
material 来自 this 站点的日历视图。
一个非常简单的包含片段的应用程序,我有一个 activity 包含一个从侧面导航抽屉打开的 CalendarView。当您打开“日志”片段时,它会将您发送到 CalendarActivity 供您选择要记录信息的日期。但是,当您执行此操作时应用程序崩溃,我发现您是否删除了行“logCalendar = (CalendarView) findViewById(R.id.logCalendar);”它不会崩溃,但显然我现在不能使用日历来获取日期。
有谁知道它崩溃的原因,或者任何其他解决方案可以为我想要实现的目标提供相同的解决方案。
谢谢。
日历活动class
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);
}
});
}
}
Class 打开 CalenderActivity class
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.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)
您发布的 logcat 解释了错误本身。看到这一行:-
java.lang.ClassCastException: com.applandeo.materialcalendarview.CalendarView cannot be cast to android.widget.CalendarView
.
您似乎导入了 android
本机 calendarView
而不是 material calendar view
。检查 CalendarActivity class
中的 imports
,您可能会发现类似这样的内容:- import android.widget.CalendarView;
删除它并将其更改为 import com.applandeo.materialcalendarview.CalendarView;
。我假设您正在使用 applandeo
material 来自 this 站点的日历视图。