当前位置:网站首页>class和getClass()的区别
class和getClass()的区别
2022-07-02 14:42:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
前几天做项目,觉得自己都开发一年多了,还没有自己封装的类,感觉真是白做了,再加上,看到自己的代码,我都不忍心看,有的时候,还需要自己去读自己写的代码,乱乱糟糟的,实在不忍心看,没办法,重现在开始吧,把自己需要的,都封装起来,用到什么的时候,在哪来用,方便,快捷
首先是自己封装的基类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的类,当然错误不一样了,当然出现问题了
我替你们踩坑了,以后封装的小伙伴注意了,别再犯我这么低级的错误了,唉
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148097.html原文链接:https://javaforall.cn
边栏推荐
- Usage of sprintf() function in C language
- 【Leetcode】13. 罗马数字转整数
- PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
- Talk about an experience of job hopping and being rejected
- 【Leetcode】14. Longest Common Prefix
- Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
- Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
- 亚马逊云科技 Community Builder 申请窗口开启
- One year is worth ten years
- Interpretation of key parameters in MOSFET device manual
猜你喜欢
![[cloud native] briefly talk about the understanding of flume, a massive data collection component](/img/2d/8c4769e97fb84e98eafb7069551341.png)
[cloud native] briefly talk about the understanding of flume, a massive data collection component

ETH数据集下载及相关问题

【Leetcode】14. 最長公共前綴

PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
![[essay solicitation activity] Dear developer, RT thread community calls you to contribute](/img/31/11409606718e0f4837f4cc572172a3.png)
[essay solicitation activity] Dear developer, RT thread community calls you to contribute

电脑自带软件使图片底色变为透明(抠图白底)

七张图,学会做有价值的经营分析

871. Minimum refueling times

Sword finger offer 26 Substructure of tree

DigiCert SSL证书支持中文域名申请吗?
随机推荐
871. 最低加油次数
Executive engine module of high performance data warehouse practice based on Impala
Deep learning image data automatic annotation [easy to understand]
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
亚马逊云科技 Community Builder 申请窗口开启
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
PWM controlled steering gear
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
C语言中sprintf()函数的用法
JS delete substring in string
OpenPose的使用
Sword finger offer 24 Reverse linked list
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
lsf基础命令
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
What if the default browser cannot be set?
移动应用性能工具探索之路
七张图,学会做有价值的经营分析
Detailed explanation of @accessories annotation of Lombok plug-in
Un an à dix ans