当前位置:网站首页>The difference between class and getClass ()
The difference between class and getClass ()
2022-07-02 17:20:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
I was working on a project the other day , I feel that I have been developing for more than a year , There is no self encapsulated class , I feel like I did it in vain , Plus , See your own code , I can't bear to see it , sometimes , You also need to read your own code , Messy , I really can't bear to see , Can't , Reappear at the beginning , Put what you need , It's all encapsulated , When to use , Where can I use it , convenient , quick
The first is the base class encapsulated by itself baseActivity, Don't bullshit , Go straight to the code ( The rest will not be posted , Only this place has mistakes )
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);
}
/**
* Initialize control or data
*/
protected abstract void initView();
/**
* Initialize click events
*/
protected abstract void setListener();
/**
* Single activity The jump
*
* @param mClass Jump class
*/
public void start_activity(Class<?> mClass) {
startActivity(new Intent(context, mClass.getClass()));
}
/**
* Jump with pass parameters
*
* @param mClass Jump class
* @param key Jump key value
* @param value Jump value value
*/
public void start_activity(Class<?> mClass, String key, String value) {
startActivity(new Intent(context, mClass).putExtra(key, value));
}
/**
* Jump with pass parameters
*
* @param mClass Jump class
* @param key Jump key value
* @param value Jump value value
*/
public void start_activity(Class<?> mClass, String key, int value) {
startActivity(new Intent(context, mClass).putExtra(key, value));
}
/**
* Jump with pass parameters
*
* @param mClass Jump class
* @param key Jump key value
* @param value Jump value value
*/
public void start_activity(Class<?> mClass, String key, Bundle value) {
startActivity(new Intent(context, mClass).putExtra(key, value));
}
/***
* Click on the blank space Hide soft keyboard
* @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);
}
/**
* according to EditText The coordinates are compared with the coordinates clicked by the user , To determine whether to hide the keyboard , Because when the user clicks EditText You can't hide
*
* @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) {
// Click on EditText Events , Ignore it .
return false;
} else {
return true;
}
}
// If the focus is not EditText It ignores , This happens when the view has just been drawn , The first focus is not on EditText On , And the user selects another focus with the trackball
return false;
}
/**
* obtain InputMethodManager, Hide soft keyboard
*
* @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);
}
}
}
I thought I encapsulated well , Very satisfied , ha-ha , I also have my own encapsulated code , In the future, it will be improved , Directly use this framework to do projects , That's not swishing , I think Kaisen
Start using activity When you jump around , It's encapsulated by itself start_activity Method , As a result, a basin of cold water poured on my head , Direct error
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.demo.neu/java.lang.Class};
have you declared this activity in your AndroidManifest.xml?
Can't find this class ? I'm already in androidManifest It's registered inside , Why does it prompt that this class cannot be found , try Google Provided startac Method , have a look
Can jump , Why , I can't jump in my package , There is no problem , hold androidManifest Delete the type inside , Use... Again Google Of startactivity Methods do jump to see , The result is wrong
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?
Why , The mistake is different , ok , It's not that the class is not registered , Let's take a look at our own packaging methods , When I looked carefully, I found , The original problem is here
/**
* Single activity The jump
*
* @param mClass Jump class
*/
public void start_activity(Class<?> mClass) {
startActivity(new Intent(context, mClass.getClass()));
}
And the lower package
/**
* Jump with pass parameters
*
* @param mClass Jump class
* @param key Jump key value
* @param value Jump value value
*/
public void start_activity(Class<?> mClass, String key, String value) {
startActivity(new Intent(context, mClass).putExtra(key, value));
}
Where is the single jump used class.getClass(), Get rid of getClass(), The successful running , You can jump , So here comes the question ,class and getClass() What's the difference ,
Careful you may have found , That's the problem
com.demo.neu/java.lang.Class
and
com.demo.XXX/com.demo.XXX.XXX.activity.LoginActivity
That's the problem , The error report in front is java Inside long type , Because in long There is no loginActivity This class , So there's a mistake , Then there is a specific class specified , That is to say, No androidMainfest There is no registration inside
One is the class inside the type , One is concrete activity Class , Of course, the mistake is different , Of course there is a problem
I stepped on the pit for you , I'll pay attention to it in the future , Don't make me such a low-level mistake again , alas
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/148097.html Link to the original text :https://javaforall.cn
边栏推荐
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- AP and F107 data sources and processing
- 剑指 Offer 25. 合并两个排序的链表
- Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
- Ocio V2 reverse LUT
- In MySQL and Oracle, the boundary and range of between and precautions when querying the date
- 【Leetcode】13. 罗马数字转整数
- 綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
- js删除字符串中的子串
- Timing / counter of 32 and 51 single chip microcomputer
猜你喜欢
ThreadLocal
Sword finger offer 27 Image of binary tree
MOSFET器件手册关键参数解读
Eth data set download and related problems
GeoServer:发布PostGIS数据源
对接保时捷及3PL EDI案例
Amazon cloud technology community builder application window opens
Eye of depth (II) -- matrix and its basic operations
【Leetcode】14. Longest Common Prefix
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
随机推荐
【Leetcode】14. 最长公共前缀
Amazon cloud technology community builder application window opens
MOSFET器件手册关键参数解读
Learning Weekly - total issue 60 - 25th week of 2022
相信自己,这次一把搞定JVM面试
Ap和F107数据来源及处理
The beginning of life
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
P6774 [noi2020] tears in the era (block)
Does digicert SSL certificate support Chinese domain name application?
Easy language ABCD sort
Ocio V2 reverse LUT
例题 非线性整数规划
Understand one article: four types of data index system
Linux Installation PostgreSQL + Patroni cluster problem
Sword finger offer 24 Reverse linked list
Sword finger offer 26 Substructure of tree
Vscode setting delete line shortcut [easy to understand]
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
Qstype implementation of self drawing interface project practice (II)