当前位置:网站首页>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
边栏推荐
- What is the difference between JSP and servlet?
- class和getClass()的区别
- Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
- GeoServer:发布PostGIS数据源
- 【Leetcode】13. 罗马数字转整数
- 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
- Amazon cloud technology community builder application window opens
- Geoserver: publishing PostGIS data sources
- Leetcode question brushing record | 933_ Recent requests
- Shutter: action feedback
猜你喜欢

博客主题 “Text“ 夏日清新特别版

Eye of depth (III) -- determinant of matrix

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

上传代码到远程仓库报错error: remote origin already exists.

默认浏览器设置不了怎么办?

871. Minimum refueling times

Amazon cloud technology community builder application window opens

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

一文看懂:数据指标体系的4大类型

linux安装postgresql + patroni 集群问题
随机推荐
executescalar mysql_ExecuteScalar()
Sword finger offer 22 The penultimate node in the linked list
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
Eye of depth (II) -- matrix and its basic operations
Visibilitychange – refresh the page data when the specified tab is visible
Linux Installation PostgreSQL + Patroni cluster problem
2、 Expansion of mock platform
The computer comes with software to make the background color of the picture transparent (matting white background)
AP and F107 data sources and processing
R and rstudio download and installation tutorial (super detailed)
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
VMware install win10 image
【Leetcode】14. Longest Common Prefix
IP address translation address segment
Timing / counter of 32 and 51 single chip microcomputer
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
一年顶十年
Eye of depth (III) -- determinant of matrix
What if the default browser cannot be set?
伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO