Novice Program Ape, in the development of the inevitable will make a variety of errors, the following are some of the problems of the development of Android some of the common mistakes, come together to see it.
1. Avoid placing multiple classes in a folder, except for one-time use of internal classes.
is a file, it is best to divide it into the main class with the same name.
2. The code length of a method should not be more than 35 lines
This is our method, don't write it too long,
It's like flipping through a few lines, or splitting it into several ways, putting a lot of common methods out of it in another function, or refactoring.
3. In principle, try not to manually modify the machine generated code
For example,the R.java class in Android projects
There are automatically generated classes, and do not modify, if these classes are related to data structure, changed the data structure, it is very hard to manually modify a lot of content, the result is now in the database, changed a field and the class again in the build again.
4.Final string instead of direct "string", the same as int
when using constants, do not directly in the When the message box pops up, go directly to print "Hello, input Error".
Do not hit these values when defining constants.
5. It is not recommended to have a direct return operation in a circular statement
Loop statement to let it, normal loop exit,return is an abnormal loop exit, non-normal case of exit.
6. Do not use goto statements
7. Consider exceptions everywhere
especially IO (input/output stream) operation, be sure to consider its exception.
The following must be Try-catch, even try several catch.
Try
{
}
catch (Exception e)
{
Todo:handle exception
}
8. Only public is required to be published, and the remainder is internal with private or other member variables.
9. Try not to provide public and protected programmer variables, use attributes instead of them
when we publish, generally we not only use public , generally we are using get,set this way to set.
This also has some advantages, in get and set, you can also do something about private variables.
Even sometimes, you only give him read only, do not give it can write.
10. Do not write business code directly in the system automatically generated functions, preferably custom functions, and then call.
Don't write like that in OnCreate.
code example:
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
int _studentcount = 5;
return _studentcount;
int _studentcount = 5;
return _studentcount;
Toast.maketext (this,this. message_warn,3000). Show ();
}
Instead, they are defined in the function, and then placed inside the call.
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.calulatestudetscount (class_on);
Toast.maketext (this,this. message_warn,3000). Show ();
}
Defined inside the function.
Call
If you want to initialize, it is the same, in defining a function.
private void Initdisplay ()
{
}
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.calulatestudetscount (class_on);
Initdisplay ();
Toast.maketext (this,this. message_warn,3000). Show ();
}
Because this is a system-generated, do not put complex logical relationships, complex relationships here, these inside as much as possible to do some organizational work.
11. Always use the interface, it is recommended to use the display interface implementation.
We all know that this implementation is divided into two ways: display and implicit. In general, say you have some business code, you still have to define it in the interface.
Because in the end you want to publish out, do not think the interface useless, all write to the class.
For some custom code, say: What to publish externally, to calculate the number of students, you define it in the relevant interface. Define this method inside, and then use these classes to implement the interface.
always have default words in the switch statement to display the information
Article Source: Wheat Academy
original link:http://www.maiziedu.com/wiki/android/error/
Several mistakes made in Android development