Android Java programming considerations

Source: Internet
Author: User

1. After creating an object:

(1) not released in proper places

(2) There is no release operation in the place where it should be released

For example, the following program:

M_progressdlg = progressdialog. Show (this, getstring (R. String. Market), getstring (R. String. is_visiting), true );

New thread (){
Public void run (){
Boolean downloadover = downloadwidgetinfo ();
If (downloadover ){
If (null! = M_progressdlg)
M_progressdlg.dismiss (); // This object is released. If it is not released, the application will be prone to errors.

If (FLAG)
Startintent ();
Finish ();
} Else {
Message MSG = mhandler. obtainmessage (muphonemain. download_fail );
Mhandler. sendmessage (MSG );
}
}
}. Start ();

The hidden danger of the above Code is that the progressdlg object is not released in else. If an error occurs, the progressdlg is always displayed on the screen, and the application will die on this interface.

2. Logical serious errors:

(1) If an activity is in a transitional role in the application, such as downloading. Therefore, it is best not to use dialog related programs in this activity. If yes, this will cause a fatal error once the activity is connected to the Internet or exceptions are caused by doing something. It is easy to see that when the application fails, the dialog is notified to display a dialog on the activity. Because the program fails, the dialog does not have the carrier of the activity context.

For example, three activities are as follows:

Activity1: Entry

Activity2: Transition, responsible for downloading network data. If the download fails, the error message "dialog" appears. Activity3.

Activity3:

In the above example, this situation occurs: After activity2 suddenly recovers from a system exception, the activity2 link network may cause a fatal error and the dalvic Virtual Machine kill activity2. at this time, your program may have caught an exception, and send a message to start a prompt box with an exception dialog. Because the context of dialog is activity2, activity2 is killed at this time. In this case, the application is terminated abnormally.

 

(2) When creating a new thread, pay special attention to whether the thread can be controlled or whether to control it. Especially when multiple threads are started, it is difficult to easily control the thread on Android just like writing a Java application. When multiple threads start, pay attention to the logic between threads. Whether the program running in multiple threads depends on the functions of the Code in other threads.

For example:

New thread1 (){

Public void run (){

Test1 (); // a list data structure in a class is created.

}

}. Start ();

New thread2 (){

Public void run (){

Test2 (); // the data structure of the list created in thread1 is used.

}

}. Start ();

The above program is a data structure created in thread 1 used by thread 2. If the first thread is not completed, an error will be reported when the second thread starts.

3. Chaotic ideas lead to chaotic code, and chaotic Code leads to a messy bug in the application.

(1) give up the functions used by many classes for public use.

(2) discard the main thread Operation Event and use multiple threads to execute the operation in disorder. This will easily lead to uncontrollable threads and unclear logic. It may cause an understanding error for the subsequent code sorting process.

(3) the logic of the application is unclear, resulting in frequent changes to the solution during application development, making it difficult to maintain the code.

(4) frequent changes to the product definition result in developers not having a clear idea, resulting in an increase in code logic errors when the project is very tight recently.

(5) focus only on the implementation of functional code without considering exception capture and failing to take measures for possible errors. This is the most common aspect of bug occurrence.

(6) Separate the code for writing the Damon small instance from the code for writing the project. It is best not to test your Damon in the project code. Your laziness can easily make the code cumbersome and ugly.

4. Conclusion:

(1) define what you want to do and build your own application logic with purpose.

(2) do not be lazy. Do not copy the code for replication. If you think that some code in a class needs to be used in another class, do not simply copy it. Think about the first one.

(3) function-based code that can be functional, and public code that can be reused to form the thought of functional component-based code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.