当前位置:网站首页>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
边栏推荐
- Smart trash can (V) - light up OLED
- Sword finger offer 26 Substructure of tree
- 智能垃圾桶(五)——点亮OLED
- 博客主题 “Text“ 夏日清新特别版
- 深度之眼(二)——矩阵及其基本运算
- Qstype implementation of self drawing interface project practice (II)
- class和getClass()的区别
- 体验居家办公完成项目有感 | 社区征文
- What is generics- Introduction to generics
- Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
猜你喜欢
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
Sword finger offer 24 Reverse linked list
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
Timing / counter of 32 and 51 single chip microcomputer
Sword finger offer 27 Image of binary tree
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
[leetcode] 14. Préfixe public le plus long
GeoServer:发布PostGIS数据源
深度之眼(二)——矩阵及其基本运算
随机推荐
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
Exploration of mobile application performance tools
linux安装postgresql + patroni 集群问题
R and rstudio download and installation tutorial (super detailed)
ThreadLocal
【Leetcode】13. 罗马数字转整数
LSF basic command
Qstype implementation of self drawing interface project practice (II)
Error when uploading code to remote warehouse: remote origin already exists
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
博客主题 “Text“ 夏日清新特别版
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
IPtables中SNAT、DNAT和MASQUERADE的含义
How openharmony starts FA of remote devices
2、 Expansion of mock platform
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
Dgraph: large scale dynamic graph dataset
Sword finger offer 27 Image of binary tree
Eth data set download and related problems
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面