java.lang.RuntimeException: Unable to start activity ComponentInfo{}java.lang.ClassCastException: co

本文介绍了一个使用Android原生组件实现的日期和时间选择器,通过DatePickerDialog和TimePickerDialog来方便地设置日期和时间,并展示了如何在Activity中集成这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.xylophone.twenty_one;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;


@RequiresApi(api = Build.VERSION_CODES.N)
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
            DatePickerDialog.OnDateSetListener,
            TimePickerDialog.OnTimeSetListener{
    Calendar c = Calendar.getInstance();
    TextView txDate;
    TextView txTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txDate = (TextView)findViewById(R.id.textView1);
        txTime = (TextView)findViewById(R.id.textView2);
        txDate.setOnClickListener(this);
        txTime.setOnClickListener(this);
    }
    public void onClick (View v){
        if (v == txDate){
            new DatePickerDialog(this,this,
                    c.get(Calendar.YEAR),
                    c.get(Calendar.MONTH),
                    c.get(Calendar.DAY_OF_MONTH))
                    .show();
        }
        else if(v == txTime){
            new TimePickerDialog(this,this,
                    c.get(Calendar.HOUR_OF_DAY),
                    c.get(Calendar.MINUTE),true)
                        .show();
        }
    }
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        txDate.setText("日期"+year+"/"+(month+1)+"/"+dayOfMonth);

    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        txTime.setText("时间"+hourOfDay+":"+minute);

    }

}
implements View.OnClickListener 

未添加View 导致打包后app闪退

报错代码为:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xylophone.twenty_one/com.xylophone.twenty_one.MainActivity}: java.lang.ClassCastException: com.xylophone.twenty_one.MainActivity cannot be cast to android.view.View$OnClickListener
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
	at android.app.ActivityThread.-wrap12(ActivityThread.java)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
	at android.os.Handler.dispatchMessage(Handler.java)
	at android.os.Looper.loop(Looper.java)
	at android.app.ActivityThread.main(ActivityThread.java)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
Caused by: java.lang.ClassCastException: com.xylophone.twenty_one.MainActivity cannot be cast to android.view.View$OnClickListener


添加view后正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值