当前位置: 首页 > >

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

发布时间:

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后正常



友情链接: