0

我有以下代码在 C 服务器和 android 客户端之间建立 TCP 连接。c服务器工作正常。但是下面的代码显示了在 Eclipse 中构建的错误。

package com.app.client.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream;
import java.io.OutputStreamWriter; 
import java.io.InputStreamReader;
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.util.Log; 

public class my_activity extends Activity
{ 
   private TextView txt;

   @Override 
   public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      Button b = (Button)findViewById(R.id.button1);
      txt = (TextView)findViewById(R.id.textView1);
      b.setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            connectSocket("Hello");

        }
      });
} 

private void connectSocket(String a){ 

    try { 
        InetAddress serverAddr = InetAddress.getByName("192.168.1.2"); 
        Log.d("TCP", "C: Connecting..."); 
        Socket socket = new Socket(serverAddr, 4444); 

        message = "1";

        PrintWriter out = null;
        BufferedReader in = null;

        try { 
            Log.d("TCP", "C: Sending: '" + message + "'"); 
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                

            out.println(message);
            while ((in.readLine()) != null) {
                txt.append(in.readLine());
            }

            Log.d("TCP", "C: Sent."); 
            Log.d("TCP", "C: Done.");               

        } catch(Exception e) { 
            Log.e("TCP", "S: Error", e); 
        } finally { 
            socket.close(); 
        } 

    } catch (UnknownHostException e) {
        Log.e("TCP", "C: UnknownHostException", e); 
        e.printStackTrace(); 
    } catch (IOException e) {
        Log.e("TCP", "C: IOException", e); 
        e.printStackTrace(); 
    }       
} 
}

构建时显示的错误是:

main cannot be resolved or is not a field   my_activity.java    /Androidclient/src/com/app/client/app   line 29 Java Problem

我该怎么做才能解决这个问题?提前致谢。

4

1 回答 1

0

您需要在您的活动中设置您的AndroidManifest.xml 。找到它并将您的主要活动添加到其中,如下所示:

 <application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name="my_activity"
              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>

这将告诉 android 系统这是您的主要活动,它应该在应用程序启动时启动。

如果您是 android 新手,我建议您阅读本文

于 2012-06-20T09:33:59.800 回答