class和getClass()的区别

来源:互联网 发布:淘宝店铺数据查询 编辑:程序博客网 时间:2024/05/16 17:47

前几天做项目,觉得自己都开发一年多了,还没有自己封装的类,感觉真是白做了,再加上,看到自己的代码,我都不忍心看,有的时候,还需要自己去读自己写的代码,乱乱糟糟的,实在不忍心看,没办法,重现在开始吧,把自己需要的,都封装起来,用到什么的时候,在哪来用,方便,快捷

首先是自己封装的基类baseActivity,不废话,直接上代码(其他的就不贴出来了,只有这个地方有错误)

package com.demo.XXX.XX.base;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;import com.demo.XXX.XXX.tools.LogUtil;import com.demo.XXX.XXX.tools.ShareUtils;/** * Created by XXX on 2016/9/26. */public abstract class BaseActivity extends Activity {    private String TAG;    protected Context context;    protected ShareUtils shareUtils;    protected LayoutInflater inflater;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        context = this;        inflater = LayoutInflater.from(context);        TAG = getClass().getName();        LogUtil.i("BaseActivity", TAG);        shareUtils = new ShareUtils(context);    }    /**     * 初始化控件或者数据     */    protected abstract void initView();    /**     * 初始化点击事件     */    protected abstract void setListener();    /**     * 单一的activity的跳转     *     * @param mClass 跳转的类     */    public void start_activity(Class<?> mClass) {        startActivity(new Intent(context, mClass.getClass()));    }    /**     * 带传递参数的跳转     *     * @param mClass 跳转类     * @param key    跳转key值     * @param value  跳转value值     */    public void start_activity(Class<?> mClass, String key, String value) {        startActivity(new Intent(context, mClass).putExtra(key, value));    }    /**     * 带传递参数的跳转     *     * @param mClass 跳转类     * @param key    跳转key值     * @param value  跳转value值     */    public void start_activity(Class<?> mClass, String key, int value) {        startActivity(new Intent(context, mClass).putExtra(key, value));    }    /**     * 带传递参数的跳转     *     * @param mClass 跳转类     * @param key    跳转key值     * @param value  跳转value值     */    public void start_activity(Class<?> mClass, String key, Bundle value) {        startActivity(new Intent(context, mClass).putExtra(key, value));    }    /***     * 点击空白处 隐藏软键盘     * @param ev     * @return     */    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {            View v = getCurrentFocus();            if (isShouldHideKeyboard(v, ev)) {                hideKeyboard(v.getWindowToken());            }        }        return super.dispatchTouchEvent(ev);    }    /**     * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏     *     * @param v     * @param event     * @return     */    private boolean isShouldHideKeyboard(View v, MotionEvent event) {        if (v != null && (v instanceof EditText)) {            int[] l = {0, 0};            v.getLocationInWindow(l);            int left = l[0],                    top = l[1],                    bottom = top + v.getHeight(),                    right = left + v.getWidth();            if (event.getX() > left && event.getX() < right                    && event.getY() > top && event.getY() < bottom) {                // 点击EditText的事件,忽略它。                return false;            } else {                return true;            }        }        // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点        return false;    }    /**     * 获取InputMethodManager,隐藏软键盘     *     * @param token     */    private void hideKeyboard(IBinder token) {        if (token != null) {            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);        }    }}

原本以为自己封装的不错,挺满意的,哈哈,我也有自己封装的代码了,以后在完善下,直接用这个框架做项目,那还不嗖嗖的,想想都觉得开森

开始在使用activity跳转的时候,用的自己封装好的start_activity方法,结果一盆凉水浇在了我的头上,直接报错

 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.demo.neu/java.lang.Class};
 have you declared this activity in your AndroidManifest.xml?

找不到这个类?我明明已经在androidManifest里边注册了啊,为啥还提示找不到这个类,试试Google提供的startac方法,看看

竟然可以跳转,那为啥,我封装的不能跳转呢,也没有啥问题啊,把androidManifest里边的那个类删除,再次用Google的startactivity方法做跳转看看,结果报错

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.demo.XXX/com.demo.XXX.XXX.
activity.LoginActivity}; have you declared this activity in your AndroidManifest.xml?
咦,错误竟然不一样,好吧,那就不是类没有注册的问题了,再看看自己封装的方法吧,仔细看的时候才发现,原来问题出在这里

    /**     * 单一的activity的跳转     *     * @param mClass 跳转的类     */    public void start_activity(Class<?> mClass) {        startActivity(new Intent(context, mClass.getClass()));    }
而下边的封装
/**     * 带传递参数的跳转     *     * @param mClass 跳转类     * @param key    跳转key值     * @param value  跳转value值     */    public void start_activity(Class<?> mClass, String key, String value) {        startActivity(new Intent(context, mClass).putExtra(key, value));    }
在单一跳转的哪里竟然使用了class.getClass(),去掉getClass(),运行成功,可以跳转,那么问题来了,class和getClass()有啥区别呢,

细心的你可能发现了,问题就在这里

com.demo.neu/java.lang.Class

com.demo.XXX/com.demo.XXX.XXX.activity.LoginActivity
这就是问题所在了,前边的报错是java里边的long类型,因为在long类型里边没有loginActivity这个类,所以出现错误,而后便是指定的一个具体的类,就是说没有在androidMainfest里边没有注册了

一个是类型里边的类,一个是具体的activity的类,当然错误不一样了,当然出现问题了

我替你们踩坑了,以后封装的小伙伴注意了,别再犯我这么低级的错误了,唉


0 0
原创粉丝点击