Cause of "error: shouldnotreachhere ()" during Android socket programming

Source: Internet
Author: User

Implement socket communication in the android project. The server uses the main method to create the serversocket. when running the startup server, an error "error: shouldnotreachhere ()" is reported ()"

Cause Analysis: in Java, the main function is used as the application interface. The life cycle of the class begins with the main method, and ends with the main method. Although Android uses the Java syntax, it does not apply to main as the program portal. When an activity is started, the oncreate () method, onstart () method, and onresume () method are used to completely start the activity, these methods are automatically called, which is the biggest difference between them and Java application. Therefore, if you create an android project. The app uses Android by default. jar is used as the runtime environment, but because we put the server serversocket startup in the main method according to the Convention, Android cannot parse it, so this error is reported, that is, the program will never be executed here, this type of error is also common.

Solution 1: isolate the server and client. Move the server-side serversocket creation to a Java project other than the android project. First, run the Java project, start serversocket, and then run the android project where the client is located.

Solution 2: Change the runtime environment of the server. Write serversocket in the main method of the Java class, select the project in package exployer on the left side of the IDE, right-click the project, and choose "Run as --> RUN configuration ", in the pop-up window, find "Java application --> serversocket class" in the Left Border, find the classpath option in the right border, find the bootstrap entries, and select android. jar, click the Remove button on the right to delete it, then click the Advanced button, and select Add
Libaby option, click OK, select JRE system library in the pop-up dialog box, click "Next", select the workplace default JRE option, and click "finish" to complete the configuration.

Solution 3: Use the activity in Android to create the server serversocket. This solution should write the server and client in different methods of the same activity, first start the server method, and then start the client method. This is because an activity in Android represents a screen. If you write the server and client in a different activity, first start the server activity and then jump to the client activity, the server activity will execute the onpause () callback method, which is equivalent to disabling the socket server.

Solution 4: Use the service in Android to create the server serversocket. Create a service to store the socket server code, and create another activity to store the Socket Client code and display the communication information. Start the service through the activity before starting the activity of the client code. Because the service in Android is running in the background, even if the activity is redirected, the service is not disabled.

In these four solutions, the first method separates the server and the client in different projects, which is not conducive to management. The third method writes the client and server code in the same class, it is messy and does not conform to the actual situation. I personally think the second and fourth schemes are preferred, especially the fourth. The second solution is mixed with the Android and Java environment processing, and the fourth solution is a pure Android application, and the service in Android is used, "Service -- activity" is more in line with the socket "server-client" mode.

Related Article

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.