Home > Mobile >  The child thread no stars. Prepare () to create a Handler is not an error
The child thread no stars. Prepare () to create a Handler is not an error

Time:10-17

This code below, in the child thread created a Handler, there is no call stars. Prepare (), however there is no error,
And, the the first test log stars. MyLooper () is null, the second test log stars. MyLooper () is not null ,
is the new Handler () also can automatically create stars ?
PS: run on the computers of the company is not an error, the same code, an error on my computer, and my computer JDK is different, but are using the android SDK to 25, the handler is android OS below, and JDK should have nothing to do?
 public class MainActivity extends the Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

InitLooperAndThread ();
}

Private void initLooperAndThread () {
New Thread () {
@ Override
Public void the run () {
The Log. I (" test ", which myLooper () + "");
MyHandler handler=new MyHandler ();
The Log. I (" test ", which myLooper () + "");
}
}.start();
}

The class MyHandler extends Handler {
@ Override
Public void handleMessage (Message MSG) {
Super. HandleMessage (MSG);
}
}
}

CodePudding user response:

Are you sure, in the company of the code is this?

CodePudding user response:

May be your machine is too be demolished

CodePudding user response:

reference 1st floor xw13782513621 response:
are you sure, in the company of the code is this?

Sure, tried several times in the morning, the code and I test the same

CodePudding user response:

Will get you this project to my email @qq.com I have a look at 1444089871,,,

CodePudding user response:

What is wrong?

CodePudding user response:

You are the real machine test or simulator? Optimization may be true on the flight

CodePudding user response:

Machine is different, or Android version

Huawei android10, print out the information below, but didn't save didn't collapse, the normal operation of the APP
E/Handler: This is not the main thread, and the caller should invoke stars. Prepare () and stars. The loop () called byandroid. OS. Handler. & lt; init> : 122 com. Test. MainActivity $MyHandler. & lt; init> : 82 com. Test. The MainActivity $1. Run: 76 & lt; Bottom of the call stack>

Another 8.1 system crash error
E/AndroidRuntime: FATAL EXCEPTION: Thread - 2
Process: com. Test, PID: 15199
Java. Lang. RuntimeException: Can 't create handler inside thread that has not called stars. Prepare ()

CodePudding user response:

Look down at the source, the structure of the Handler method, is sure to throw an exception, is no exception, you should be a simulator or real machine of special circumstances,
  • Related