我有以下代码在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
我可以做些什么来解决这个问题?提前谢谢。
答案 0 :(得分: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新手,我建议您阅读this