当前位置:网站首页>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
边栏推荐
- Deep learning image data automatic annotation [easy to understand]
- 深度之眼(三)——矩阵的行列式
- The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
- class和getClass()的区别
- Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
- 2022 interview questions
- Dstat use [easy to understand]
- 上传代码到远程仓库报错error: remote origin already exists.
- Idea2021.1 installation tutorial
- Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
猜你喜欢

Dgraph: large scale dynamic graph dataset

TCP congestion control details | 2 background

871. Minimum refueling times

七张图,学会做有价值的经营分析

Eye of depth (II) -- matrix and its basic operations

宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权

剑指 Offer 27. 二叉树的镜像

智能垃圾桶(五)——点亮OLED

MOSFET器件手册关键参数解读

Eth data set download and related problems
随机推荐
Tech talk activity preview | building intelligent visual products based on Amazon kVs
Sword finger offer 24 Reverse linked list
Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
对接保时捷及3PL EDI案例
linux安装postgresql + patroni 集群问题
深度之眼(三)——矩阵的行列式
Domestic relatively good OJ platform [easy to understand]
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
Eye of depth (II) -- matrix and its basic operations
How openharmony starts FA of remote devices
Schoolbag novel multithreaded crawler [easy to understand]
上传代码到远程仓库报错error: remote origin already exists.
亚马逊云科技 Community Builder 申请窗口开启
一年顶十年
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
Linux Installation PostgreSQL + Patroni cluster problem
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
关于我
例题 非线性整数规划
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]