1

我目前正致力于在 Android 上编写一个简单的消息应用程序。我在一个名为 的类中有服务器runServer,它运行完美。然后我在我的mainActivity. 我有一个button,其中有一个onclick="startClient". 但每当我点击 时button,我都会收到错误消息。

03-11 13:01:40.622: E/AndroidRuntime(718): java.lang.IllegalStateException: 
Could not find a method startClient(View) in the activity class com.example.datorkomprojekt.MainActivity 
for onClick handler on view class android.widget.Button with id 'connect'

我能做些什么来解决这个问题?

主要活动

public class MainActivity extends Activity {

private Socket socket;
private ObjectInputStream ois;
private DataOutputStream dos;


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

    runOnUiThread(new Runnable(){
        public void run(){
        EditText et = (EditText) findViewById(R.id.message_id);
        String message = et.getText().toString();
        EditText et1 = (EditText) findViewById(R.id.messages);
        et1.append(message);
        }
    });
}

    public void startClient() {
        socket = new Socket();
        InetSocketAddress ipPort = new InetSocketAddress("10.0.2.15", 4444);
        try {
            socket.connect(ipPort);
            dos = new DataOutputStream(socket.getOutputStream());
        } catch (Exception e) {
        }
        Thread thread = new Thread();
        thread.start();
    }

}

服务器类

public class RunServer {

private ServerSocket server;
private Socket socket;
private DataInputStream dis;
private int port;

public RunServer(int port){
    this.port = port;
    Thread connectThread = new Thread( new Connect() );
    connectThread.start();
}

private class Connect implements Runnable {

public void run() {

    try {
        server = new ServerSocket(port);
    System.out.println("Server running");
    while (true) {
        socket = server.accept();
            Thread clientThread = new Thread( new TalkToClient( socket ) );
            clientThread.start();
            System.out.println("someone connected");
    }
        } catch (IOException ex) {
            Logger.getLogger(RunServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    try {
        server.close();
        } catch( Exception e ) {}
    }
}

private class TalkToClient implements Runnable{

    private Socket socket;
    public TalkToClient( Socket socket ) {
    this.socket = socket;
    }

public void run() {

    String clientMsg;
    try {
         dis = new DataInputStream(socket.getInputStream());
        while ((clientMsg = dis.readUTF()) != null) {
            if (clientMsg.length() > 0) {

            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }

}

}

public static void main(String[] args) {
    new RunServer(4444);
}

}

XML 布局

<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:background="@drawable/bakgrund"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/ip"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white" />

<EditText
    android:id="@+id/ip_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:background="@color/white"
    android:ems="10"
    android:hint="@string/get_ip"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ip_id"
    android:text="@string/port"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white" />

<EditText
    android:id="@+id/port_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView2"
    android:background="@color/white"
    android:ems="10"
    android:hint="@string/get_port"
    android:inputType="number" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/port_id"
    android:layout_below="@+id/port_id"
    android:text="@string/message"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white" />

<EditText
    android:id="@+id/message_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView3"
    android:background="@color/white"
    android:ems="10"
    android:hint="@string/get_message" />

<Button
    android:id="@+id/connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/message_id"
    android:text="@string/connect"
    android:textColor="@color/white"
    android:onClick="startClient" />

<Button
    android:id="@+id/send"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/connect"
    android:layout_alignBottom="@+id/connect"
    android:layout_toRightOf="@+id/connect"
    android:text="@string/send"
    android:textColor="@color/white" />

<TextView
    android:id="@+id/status_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/connect"
    android:layout_below="@+id/send"
    android:layout_marginTop="23dp"
    android:text="@string/status"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white" />

<EditText
    android:id="@+id/messages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/status_id"
    android:background="@color/white"
    android:ems="10"
    android:inputType="textMultiLine" />

</RelativeLayout>
4

4 回答 4

5

public void startClient(View v)..这将是方法的签名

更新:

由于每个人都发布相同的答案,我想我最好改进一下我的答案。虽然单行已经解决了你的问题如果你有空闲时间,你可以看到以下链接..在那里你可以找到关于处理点击事件的不同方法的清晰概念

http://www.remwebdevelopment.com/dev/a69/Different-Ways-To-Handle-Clicks.html

http://smartcloudblog.blogspot.com/2011/09/android-onclicklisteners-vs.html

于 2013-03-11T13:10:14.530 回答
3

当您在 XML 中定义按钮的单击方法时,按钮本身作为参数传递给该方法。

您有一个startClient()方法,但 Android 正在寻找一种startClient(View v)方法。更改您的方法以匹配此签名,它将起作用。

于 2013-03-11T13:12:54.703 回答
1

更改public void startClient()public void startClient(View v)

于 2013-03-11T13:11:40.597 回答
1

改变这个:

public void startClient() {

和:

public void startClient(View view) {

方法 onClick 带有一个参数,即视图。

于 2013-03-11T13:12:05.103 回答