在我的 android 应用程序中,我有一个套接字,但是当我旋转屏幕时,我必须保持套接字连接,才能将消息发送到服务器。所以我决定这样做:
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;
}
}
在我的活动和线程中使用它的全局套接字。因为在某些情况下我必须能够打开和关闭连接。
例如,我尝试像这样访问 clientSocket:
Socket c = new Socket(IP, puerto);
((clientSocket)this.getApplication()).setSocket(c);
但我有这个错误:
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)
这是我的 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>
谢谢!