登录 立即注册
安币:

安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户

错误提示为Cannot cast from View to DatePicker[复制链接]

protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.date_picker);
    datePicker=(DatePicker)findViewById(R.id.datePicker);
}

错误提示为Cannot cast from View to DatePicker
同样的代码在我室友哪里却没出问题,
求大神解惑!!!

xqwwb 370查看 · 3回复 · 2018-12-2 23:29 有用(0
共3个回答
lilongfei0504 2018-12-3 12:48 回复

DatePicker在哪个包里我忘记了,不过我觉得你应该是没有引入这个类所在的包或者SDK版本的问题。
在你出问题的机器上打开这个layout布局文件,然后鼠标ctrl +左键点击这个类控件,看看能不能导航到对应的类定义里。

还有一个可能出问题的地方是 这个布局文件有多个版本适配,其中有一个是有问题的

经济 2018-12-26 09:49 回复

package com.sz.easyway.speakerpro.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TimePicker;

import com.sz.easyway.speakerpro.R;

import java.util.ArrayList;
import java.util.List;

public class MyTimePickerDialog {

private Context mContext;
private AlertDialog.Builder mAlertDialog;
private int mHour, mMinute;
private TimePickerDialogInterface timePickerDialogInterface;
private TimePicker mTimePicker;
public int mID;

public MyTimePickerDialog(Context context) {
    super();
    mContext = context;
    timePickerDialogInterface = (TimePickerDialogInterface) context;
}

private View initTimePicker() {
    View inflate = LayoutInflater.from(mContext).inflate(R.layout.datepicker_layout, null);
    mTimePicker = (TimePicker) inflate.findViewById(R.id.dateAndTimePicker_timePicker);
    mTimePicker.setIs24HourView(true);
    resizePikcer(mTimePicker);
    return inflate;
}

/**
 * 创建dialog
 *
 * @param view
 */
private void initDialog(View view) {
    mAlertDialog.setPositiveButton(R.string.button_positive_text,
            new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                        getTimePickerValue();
                    timePickerDialogInterface.positiveListener(mID);

                }
            });
    mAlertDialog.setNegativeButton(R.string.button_negative_text,
            new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    timePickerDialogInterface.negativeListener(mID);
                    dialog.dismiss();
                }
            });
    mAlertDialog.setView(view);
}

/**
 * 显示时间选择器
 */
public void showTimePickerDialog(int Id) {
    mID=Id;
    View view = initTimePicker();
    mAlertDialog = new AlertDialog.Builder(mContext);
    mAlertDialog.setTitle(R.string.selection_period);
    initDialog(view);
    mAlertDialog.show();

}

/*
* 调整numberpicker大小
*/
private void resizeNumberPicker(NumberPicker np) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(120,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 0, 10, 0);
    np.setLayoutParams(params);
}

/**
 * 调整FrameLayout大小
 *
 * @param tp
 */
private void resizePikcer(FrameLayout tp) {
    List<NumberPicker> npList = findNumberPicker(tp);
    for (NumberPicker np : npList) {
        resizeNumberPicker(np);
    }
}

/**
 * 得到viewGroup里面的numberpicker组件
 *
 * @param viewGroup
 * @return
 */
private List<NumberPicker> findNumberPicker(ViewGroup viewGroup) {
    List<NumberPicker> npList = new ArrayList<>();
    View child = null;
    if (null != viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            child = viewGroup.getChildAt(i);
            if (child instanceof NumberPicker) {
                npList.add((NumberPicker) child);
            } else if (child instanceof LinearLayout) {
                List<NumberPicker> result = findNumberPicker((ViewGroup) child);
                if (result.size() > 0) {
                    return result;
                }
            }
        }
    }
    return npList;
}

public int getMinute() {
    return mMinute;
}

public int getHour() {
    return mHour;
}

/**
 * 获取时间选择的值
 */
private void getTimePickerValue() {
    // api23这两个方法过时
    mHour = mTimePicker.getCurrentHour();// timePicker.getHour();
    mMinute = mTimePicker.getCurrentMinute();// timePicker.getMinute();
}

public interface TimePickerDialogInterface {
    public void positiveListener(int ID);
    public void negativeListener(int ID);
}

}

这个是启动
mTimePickerDialog = new MyTimePickerDialog(TimerActivity.this);
mTimePickerDialog.showTimePickerDialog(R.id.time);

xkxtmy 2019-1-10 10:04 回复

检查下是不是声明的变量类型有问题,要不然就是你的layout中使用的控件有问题

需要登入后才可以评论 登录 | 立即注册

联系我们
关闭
合作电话:
15618560077
Email:
805941275@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 广东互联网违法和不良信息举报中心|中国互联网举报中心|下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

返回顶部 返回版块