Android客户端错误 - java.lang.IllegalStateException:无法执行活动的方法

时间:2015-01-18 22:44:26

标签: android sockets android-studio client android-logcat

我正在构建一个简单的客户端 - 服务器系统。我已经构建了一个Java服务器&一个在我的设备上运行的android客户端(都连接到同一个局域网)。

当我只是尝试设置连接时(通过单击按钮),客户端失败并显示错误

-"java.lang.IllegalStateException: Could not execute method of the activity"

在其他问题的答案中,有人说它可能与AsyncTask有关,但我不确定。

服务器代码:

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class Server { 
        public static void startConnection(int portNum)
        {   
            ServerSocket serverSocket=null;
            Socket clinetSocket=null;
            ObjectOutputStream serverOut=null;
            ObjectInputStream serverIn=null;
            String message=null;

            //create a socket
            try{
                serverSocket=new ServerSocket(portNum);
            } 
            catch (IOException e) {
                e.printStackTrace();
            } 
            System.out.println("Waiting for connection...");
            try{
                clinetSocket = serverSocket.accept();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            System.out.println("connected to "+ clinetSocket.getInetAddress().getHostName());

        }

Android客户端代码:

    Main.java :

    package com.example.user_pc.myapplication;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import java.net.Socket;
    import java.io.IOException;
    import java.net.UnknownHostException;
    import java.net.SocketException;
    import android.util.Log;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class Main extends ActionBarActivity {

        TextView text;

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

            text= (TextView) findViewById(R.id.textView1);
            text.setText("");
        }

        public void pushTheButton(View v)
        {
            Button button = (Button) findViewById(R.id.button);
            createSocket();
        }
        public void createSocket()
        {
            Socket sock = null;
            String dstIP = "192.168.2.103";//server ip
            int dstPort = 9632;


             try
            {
                sock = new Socket(dstIP, dstPort);
            }

            catch(SocketException ie)
            {
                Log.wtf("SocketException",ie);
            }

            catch(UnknownHostException ie)
            {
                Log.wtf("UnknownHostException",ie);
            }
            catch(IOException ie)
            {
                Log.wtf("IOException",ie);
            }

    }

        public static void main(String[] args) 
        {
            int portNum=9632;
            startConnection(portNum);
        }

    ``}

的manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user_pc.myapplication" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main"
    android:onClick="pushTheButton">

    <Button
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:text="Click to connect"
        android:id="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="96dp"
        android:onClick="pushTheButton"
        />
</RelativeLayout>
  • logcat的

    01-19 16:28:02.425 10779-10779 / com.example.user_pc.myapplication E / AndroidRuntime:FATAL EXCEPTION:main java.lang.IllegalStateException:无法执行活动的方法         在android.view.View $ 1.onClick(View.java:3599)         在android.view.View.performClick(View.java:4204)         在android.view.View $ PerformClick.run(View.java:17355)         在android.os.Handler.handleCallback(Handler.java:725)         在android.os.Handler.dispatchMessage(Handler.java:92)         在android.os.Looper.loop(Looper.java:137)         在android.app.ActivityThread.main(ActivityThread.java:5226)         at java.lang.reflect.Method.invokeNative(Native Method)         在java.lang.reflect.Method.invoke(Method.java:511)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)         at dalvik.system.NativeStart.main(Native Method)  引起:java.lang.reflect.InvocationTargetException         at java.lang.reflect.Method.invokeNative(Native Method)         在java.lang.reflect.Method.invoke(Method.java:511)         在android.view.View $ 1.onClick(View.java:3594) 在android.view.View.performClick(View.java:4204) 在android.view.View $ PerformClick.run(View.java:17355) 在android.os.Handler.handleCallback(Handler.java:725) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method)  引起:android.os.NetworkOnMainThreadException         在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)         在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)         在libcore.io.IoBridge.connectErrno(IoBridge.java:127)         在libcore.io.IoBridge.connect(IoBridge.java:112)         在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)         在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)         在java.net.Socket.startupSocket(Socket.java:566)         在java.net.Socket.tryAllAddresses(Socket.java:127)         在java.net.Socket。(Socket.java:177)         在java.net.Socket。(Socket.java:149)         在com.example.user_pc.myapplication.Main.createSocket(Main.java:50)         在com.example.user_pc.myapplication.Main.pushTheButton(Main.java:35) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在android.view.View $ 1.onClick(View.java:3594) 在android.view.View.performClick(View.java:4204) 在android.view.View $ PerformClick.run(View.java:17355) 在android.os.Handler.handleCallback(Handler.java:725) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:1)

正如我所说的那样。你得到一个NetworkOnMainThreadException。您需要在后台线程中进行网络I / O.您的createSocket()方法必须启动一个新的后台线程来完成这项工作。或者使用AsyncTask