Home > Mobile >  Summary - novice common misconceptions about - JAVA Android project.
Summary - novice common misconceptions about - JAVA Android project.

Time:09-22

1, null problem
When object (basic data types, define the Class object) declare only not instantiated, it is null.
Such as:
 
The Date a;//at this point to a distribution in the stack a space saving a variable, a null,
A=new Date ();//create a Date object on the heap at this time the data space, and the space of the first address assigned to a,
Date b=a;//references. A, b will save the address assigned to b, in a single thread, a, b change synchronization, which change the data in the b, at this time a data synchronization in change,

In the debug, often thrown null reference problem,
the reason for this is that : when the object is null, unable to perform any method, common is to perform String. The clear (), String. The add (), etc.,
solution : when a null data need to operate, first to determine if statement

2, add and remove dynamically update problem fragments,
When using ViewPage + (FragmentList - FragmentAdapter), fragments of dynamic update problems, cause the reason was that the source of FragmentPagerAdapter instantiateItem () method, which based on the tag to find corresponding fragments, if found, then attach operation by the current Transaction, the fragments will be displayed, if not found, use the getItem () from your list of fragments and the Transaction for the add operation to get a (oneself see FragmentAdapter source to know what is going on, very clear and simple, if compare lazy reference analysis: https://blog.csdn.net/lyabc123456/article/details/79797552
Online tutorial is through the Transaction deleted fragments, but this method exists problems or not explain complete, through debugging found its deletion fragments of logic as shown in the figure below, the deleted fragments, but the location is still exists, fragments are in an empty after the reorder fragments,

so cause when add a Fragment, the third position will directly attach an empty fragments, the addition of fragments is invalid,
Solution: to remove fragments after adding the demand of the fragments, and delete operations should be directly through the Transaction to delete all the fragments of memory, object to instantiate FragmentAdapter refresh memory, can avoid the existence of empty fragments,
For fragments content updates, the fragments of direct access FragmentList, use the Handle to modify the content in the main thread it can update the UI,

3,==
In the String is equal conditions, the misuse of "==" equal judgment bugs,
problem reasons: "==" than to teach is the address of two not null object, use the==judge are used for general data types and object is null, the String should be used when the two String objects for judgment. The equals () method, the equals () method to compare the two objects, the contents of the values,
lead to consequences: won't appear abnormal program, when can lead to logic errors,
solution : use the equals () method to compare whether equal,

4, not the UI thread change UI components
UI creation and modification of the interface are all in the UI thread, the interface has been created, various events, such as click event processing method is in the UI thread, so when can modify the UI components, but when you need to modify the UI components in the other thread's context, such as a new network request threads, after get the data update UI components, according to amend at this time of the UI components need to handle run in the main thread,
Encountered in the weather module two network request thread update UI components in operation, the first is to request city, with the returned list data update RecycleAdapter city, the second is the request of weather, with return to weather data update fragments, as part of the update operation is not making mistakes on the UI thread,
Solution: create a Handle in the main thread, use the Handle. The post () or the Handle. The handleMessage () will update the UI components operating in other threads on the UI thread, the Handle of the two methods of using see: https://blog.csdn.net/mountain_hua/article/details/81291677
About change of UI components include (1) common UI component content changes; (2) the deletion or omission of some UI components and the visible property is set; (3) the notifydatasetchanged (); Like a list of changes in the Adapter recycleAdatper in (the weather), after calling notifydatasetchanged (); Should be put in the main thread, or you will go wrong, in the actual process encountered after the network request thread UI change sometimes don't change, like the thread of timing problem,
anyway all involved in the UI thread for any revision to the UI should be put in the main thread ,

5, multi-threaded Shared data synchronization
& lt; This section only as a personal project problem section, estimated that only I can read write what & gt;
& lt; In short data sharing, multithreading must keep the data under the condition of "visibility", this is due to the principle of Java memory mechanism, specific instructions can directly see the given reference links & gt;
In weather module, in order to obtain the interface shows all the weather information, need to request multiple urls, repeatedly updated a Fragment on a specific location on the content of different location, and the response speed of the network request is different, the interface of the update order cannot be guaranteed, to ensure the sharing of code, the fragments of different positions on the updated using the same method, and use the same Shared data types, so they use the same object in multiple threads WeatherBean, so must make the pledge that we shall share data visibility, otherwise it will cause error,
Question: in the program data is stored in main memory, each thread has its own working memory, when threads need to use a Shared data, to data in main memory copy to their working memory, after operation can use copy data, rather than use the data in the main memory, such as:
Shared data WeatherBean: share={TMP=null, air=null};
Thread 1 and thread 2 access to share at the same time, the two threads read share respectively, and the share memory in their working memory, and then thread 1 gained the TMP data of 10, assigned to share TMP, at this time in thread 1 share={TMP=10, air=null}, and write down the value of the TMP back to main memory, but the working memory in the thread 2 share={TMP=null, air=null}, when a thread 2 for the air data as "fine", its share={TMP=null, air="clear"}, thread 1 fragments. After get the data update (share), the fragments of TMP showed 10, air show empty, thread 2 fragments. After get the data update (share), the fragments of TMP showed empty air show "fine", so lead to fragments cannot update correctly,
solution : use volatile modifications need to share data, guarantee the "visibility" of the data, the use of volatile:
https://www.cnblogs.com/kubidemanong/p/9505944.html
https://blog.csdn.net/mccand1234/article/details/91345168

  • Related