Cattle off network do problems 5.29

(1) what will happen after the following code to run

public classMainActivity extends Activity implements OnClickListener

{

   private Button mBtnLogin = (Button) findViewById(R.id.btn_login);

   private TextView mTextViewUser;

  

   @Override

   protected void onCreate(BundlesavedInstanceState)

   {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mTextViewUser = (TextView) findViewById(R.id.textview_user);

        mBtnLogin.setOnClickListener(this);

        new Thread()

        {

            @Override

            public void run()

            {

                mTextViewUser.setText(10);

            }

        }.start();

   }

  

   @Override

   public void onClick(View v)

   {

        mTextViewUser.setText(20);

   }

}

Resolution:

Code found:

1, the first being given NullPointerException, it is privateButton mBtnLogin = (Button) findViewById (R.id.btn_login); this position, the first to successfully loaded the layout to obtain the corresponding button component object;

2, modify NullPointerException error before running error Resources $ NotFoundException, in mTextViewUser.setText (10); this position (thought it would first check the onclick method in the setText (), but is actually run () in the setText ()) to change a character string;

3, modify the above error before running error Resources $ NotFoundException, this time to turn mTextViewUser.setText (20); the position of;

4, modify the above error and then run, there is no error, the program runs successfully, click on the button by the TextView 10 becomes 20, said that good can not update the UI components in a non-UI thread in it? Look after someone else's blog, finally found the answer, in fact, non-UI thread can be refreshed UI, provided that it should have its own ViewRoot, ViewRoot in onResume () in addview () was created, it is in onResume () checks whether the UI thread, usually in the onCreate () can update the UI by sub-thread, but officials do not recommend it, because Android UI operations are not thread-safe.

PS: Further, the test can be run under the above code () in the setText () preceded by a Thread.sleep (2000), let a thread sleeps 2-3 seconds, the error will ViewRootImpl $ CalledFromWrongThreadException, description has checked in non-UI thread to update UI.

(2) some of which can be cross-process in Andrews

Four ways:

1. Access other applications Activity

2.Content Provider 

3. Broadcast (Broadcast) 

4. AIDL Service

(3) AOP and understanding of OOP

AOP and OOP is a methodology can also be said to design patterns, ways of thinking, the theoretical rules and so on. 
AOP can not replace OOP, OOP is obejct abstraction, while AOP is concern abstraction, the former is mainly abstract objects, such as certain types of business object abstract public interface, business logic is encapsulated report objects, more focus on certain common objects have in common abstract behavior, such as reporting module requires special report business logic package, other modules need other logical abstraction, while AOP is a common behavior dispersed in each module of the abstract, that is the focus of abstraction. Some system-level problems or total service independent thinking and many existing functions together, you can use AOP, such as abnormal information processing mechanisms of the unified custom exception information is written to the response stream and then to the front display, operated by the user logging behavior the methods, to do these things with OOP is a good interface, call around, but sometimes find most of the logic module calls too consistent, and no great relationship with the core business, may be open to independent, so deal people deal with the core business to focus on core businesses, separation of concerns, and naturally the code more independent and easier to debug analysis, more good maintenance. 
OOP core business still have to come into play, with the focus AOP is not the same, the former kind of vertical abstract sense, the latter is transverse abstract feeling, just to add to OOP AOP, no substitutes.

(5) 4 under the category java concurrent packet

A, Semaphore: class, a number of control resources may be accessed simultaneously;

B, ReentrantLock: class, monitor lock having the implicit method and using synchronized statement accessed the same basic behavior and semantics, but more powerful;

C, Future: the interface, showing the results of an asynchronous computation;

D, CountDownLatch: class, multiple threads may wait for a thread to complete the task classes.

 

(6) can handle characters are Unicode character stream related classes

Briefly, the character stream is encoded byte stream in accordance with the desired set of parsing the byte stream obtained

It can be understood as a character stream byte stream + = codeset

And so the character stream-related classes have the ability to manipulate code set (unicode) of.

The suffix is ​​the Stream is a stream of bytes, the other is the character stream.

Byte stream:

InputStream   
| - FileInputStream (basic file stream)   
| - BufferedInputStream   
| - DataInputStream   

|-- ObjectInputStream

Jifuryu

Reader 
| - InputStreamReader (byte-> char bridge) 
| - BufferedReader (common) 
Writer 
| - OutputStreamWriter (char-> byte bridge) 
| - BufferedWriter 
| - PrintWriter (Common)

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41673515/article/details/90669748