Android projects that can be run in java projects cannot be run.

Source: Internet
Author: User

Implement socket communication in the android project. The server creates a serversocket using the main method. when running the startup server, an error "error: shouldnotreachhere ()" is returned ()".
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 finally the main method. Android uses Java syntax, but does not use main as the program portal. When an activity is started, it must go through the oncreate () method, onstart () method, and onresume () method () methods are fully started, and these methods are automatically called, which is the biggest difference from Java application running. So if you create an android
Project, the program 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, so "error:
Shouldnotreachhere () "error, which means that the program will never be executed here. This error is also common. For example, after defining a return statement, you can add a system. out. like println ("XXXX"), return indicates that the program has ended, and the subsequent System statement cannot be executed because the program cannot be parsed according to the Java syntax.
Solution 1: isolate the server and client. Move the server-side serversocket creation to a Java project other than the android project. 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, and find the bootstrap
Entries, select Android. jar, click the Remove button on the right to delete it, 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, then the server activity will execute the onpause () callback method, which is equivalent to closing 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 you jump to the activity, 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.

 

The above articles are searched from the internet. I have already written a second solution;

The practice is as follows:

(1) Right vertex entry class (containing the main method) ==> run invocations ==> Java application ==> new ==>

(2) Select the corresponding projectname and the classname to run in the standard main,

(3) Select the bootstrap entries in the metrics classpath and click advance ==> add library ==> JRE system library from the right vertex, then click "up" in the JRE system library and press "OK ".

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.