当前位置:网站首页>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
边栏推荐
猜你喜欢

伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO

linux下配置Mysql授权某个用户远程访问,不受ip限制

对接保时捷及3PL EDI案例

Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP

13、Darknet YOLO3

剑指 Offer 27. 二叉树的镜像
![John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])

Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes

Digital IC hand tearing code -- voting device

几行代码搞定RPC服务注册和发现
随机推荐
PWM breathing lamp
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
Use the API port of the bridge of knowledge and action to provide resources for partners to access
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
Error when uploading code to remote warehouse: remote origin already exists
Easy language ABCD sort
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
二、mock平台的扩展
P6774 [NOI2020] 时代的眼泪(分块)
jsp 和 servlet 有什么区别?
VMware install win10 image
电脑自带软件使图片底色变为透明(抠图白底)
Vscode setting delete line shortcut [easy to understand]
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
【Leetcode】13. 罗马数字转整数
Role and function of uboot
13、Darknet YOLO3
Tech talk activity preview | building intelligent visual products based on Amazon kVs
Sword finger offer 26 Substructure of tree