当前位置:网站首页>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
边栏推荐
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- 畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
- The impact of telecommuting on all aspects of our experience | community essay solicitation
- 伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
- In MySQL and Oracle, the boundary and range of between and precautions when querying the date
- What will you do after digital IC Verification?
- [leetcode] 14. Préfixe public le plus long
- 酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
- 剑指 Offer 25. 合并两个排序的链表
- uboot的作用和功能
猜你喜欢
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
ThreadLocal
如何与博格华纳BorgWarner通过EDI传输业务数据?
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
871. 最低加油次数
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
随机推荐
Geoserver: publishing PostGIS data sources
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
Day 18 of leetcode dynamic planning introduction
MOSFET器件手册关键参数解读
IP address translation address segment
Dgraph: large scale dynamic graph dataset
2、 Expansion of mock platform
学习周刊-总第60期-2022年第25周
Exploration of mobile application performance tools
The macrogenome microbiome knowledge you want is all here (2022.7)
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
How openharmony starts FA of remote devices
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
PWM breathing lamp
Method of C language self defining function
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
What is the difference between JSP and servlet?
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
如何与博格华纳BorgWarner通过EDI传输业务数据?
人生的开始