当前位置:网站首页>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.Classand
com.demo.XXX/com.demo.XXX.XXX.activity.LoginActivityThat'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
边栏推荐
- Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
- Interpretation of key parameters in MOSFET device manual
- Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
- Timing / counter of 32 and 51 single chip microcomputer
- What is generics- Introduction to generics
- LeetCode:1380. Lucky number in matrix -- simple
- Method of C language self defining function
- The computer comes with software to make the background color of the picture transparent (matting white background)
- QStyle实现自绘界面项目实战(二)
- Leetcode question brushing record | 933_ Recent requests
猜你喜欢

Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan

MOSFET器件手册关键参数解读

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

相信自己,这次一把搞定JVM面试

Eye of depth (III) -- determinant of matrix
![[error record] error -32000 received from application: there are no running service protocol](/img/6c/66099650de46cac88b805e6cfb90b9.jpg)
[error record] error -32000 received from application: there are no running service protocol

The macrogenome microbiome knowledge you want is all here (2022.7)

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
![[shutter] dart data type (dynamic data type)](/img/6d/60277377852294c133b94205066e9e.jpg)
[shutter] dart data type (dynamic data type)
随机推荐
深度之眼(二)——矩阵及其基本运算
【Leetcode】14. 最长公共前缀
Dstat use [easy to understand]
Experience home office, feel the completion of the project | community essay solicitation
Ap和F107数据来源及处理
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
Sword finger offer 22 The penultimate node in the linked list
How to quickly distinguish controlled components from uncontrolled components?
Easy language ABCD sort
Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
什么是敏捷开发流程
详细介绍scrollIntoView()方法属性
寒门再出贵子:江西穷县考出了省状元,做对了什么?
ThreadLocal
The beginning of life
移动应用性能工具探索之路
【Leetcode】13. Roman numeral to integer
Linux Installation PostgreSQL + Patroni cluster problem
博客主题 “Text“ 夏日清新特别版
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程