Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:26:46+00:00 2026-06-06T06:26:46+00:00

in my android application I have a socket, but when I rotate the screen,

  • 0

in my android application I have a socket, but when I rotate the screen, I have to keep the socket connected, to send messages to a server. So I decided to do this:

package pfg.nao.naoControler;

import java.io.IOException;

import java.net.Socket;

import java.net.UnknownHostException;

import android.app.Application;

public class clientSocket extends Application{

public Socket clientSocket;

@Override
public void onCreate() 
{
    super.onCreate();
}

public void setSocket(Socket socket) throws UnknownHostException, IOException{

    clientSocket = socket;
}

public Socket getSocket() throws UnknownHostException, IOException{

    return clientSocket;
}

}

A global socket to use it in my activities and threads. Because I have to be able to open and close the connection in some cases.

For example, I try to access to clientSocket like this:

Socket c = new Socket(IP, puerto);
((clientSocket)this.getApplication()).setSocket(c);

But I have this error:

06-24 22:25:18.217: E/AndroidRuntime(25267): FATAL EXCEPTION: main
06-24 22:25:18.217: E/AndroidRuntime(25267): java.lang.ClassCastException: android.app.Application
06-24 22:25:18.217: E/AndroidRuntime(25267):    at pfg.nao.naoControler.NaoControlerActivity.onOptionsItemSelected(NaoControlerActivity.java:160)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at android.app.Activity.onMenuItemSelected(Activity.java:2312)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:769)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at android.view.View$PerformClick.run(View.java:9262)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at android.os.Handler.handleCallback(Handler.java:587)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at android.os.Looper.loop(Looper.java:130)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at android.app.ActivityThread.main(ActivityThread.java:3744)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at java.lang.reflect.Method.invoke(Method.java:507)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-24 22:25:18.217: E/AndroidRuntime(25267):    at dalvik.system.NativeStart.main(Native Method)

Here it is my android manyfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pfg.nao.naoControler"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".NaoControlerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SettingsActivity"
        android:label="@string/settings" >
    </activity>
</application>
<application
    android:name=".clientSocket"
    android:label="@string/cliente" >
</application>

</manifest>

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:26:48+00:00Added an answer on June 6, 2026 at 6:26 am

    What I’d probably do (just based on the info you’ve given) is make the Socket accessible via static method. This way you don’t have to worry about whether or not the Application is initialized, or if you’re calling the right Application object.

    Something like this (renamed a couple things to avoid confusion…):

    public class clientSocket extends Application {
        private static Socket localSocket;
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        public static void setSocket(Socket socket) throws UnknownHostException, IOException {
            localSocket = socket;
        }
    
        public static Socket getSocket() throws UnknownHostException, IOException {
            // Check here if it's null; if it is, then initialize it, or throw some exception, or something.
            return localSocket;
        }
    }
    

    Then, you can initialize it like this:

    Socket c = new Socket(IP, puerto);
    clientSocket.setSocket(c);
    

    And you can call it like this:

    Socket c = clientSocket.getSocket();
    

    Keep in mind: this method will only work if you have just one Socket that you are sharing between classes. If you have multiple, you would have to array them, have multiple classes (with their own Sockets), or multiple static Socket objects in your class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an android application that needs to connect to a socket server.
I'm programming an Android application allows to send messages between devices which are connected
I have an application that needs to send messages, repeatedly to the server. I'm
I have an client server application on Android.And I have to send data from
Have a server-socket running in an android application, which I debug using the emulator.
i'm building an android application which have a chat. in this chat i each
i am developing an arabic android application and i have the compatibility package referenced
I have an android application which (over the network) communicates with a desktop server
I have a problem with socket send (or write) function on android. There is
I have an Android application where I'm trying to send a picture to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.