我尝试使用套接字连接将java桌面程序中的字符串发送到android。当用户单击Android应用程序中的按钮时,它将显示来自Java应用程序的消息。当我点击按钮时,它会显示java.lang.IllegalStateException。我可以在这个网站上找到很多像这样的问题,但没有人符合我的要求。
Android代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity
{
private Socket client;
private InputStreamReader isr;
private BufferedReader bf;
private String message;
private AsyncClass ac;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
class AsyncClass extends AsyncTask<Void, Void,Void>
{
protected Void doInBackground(Void... params)
{
hardtask();
return null;
}
public void hardtask()
{
try
{
client=new Socket("10.0.2.2",7777);
isr=new InputStreamReader(client.getInputStream());
bf=new BufferedReader(isr);
message=bf.readLine();
Log.v("Message", message);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void onClick(View view)
{
ac.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
logcat的
02-27 02:02:02.191: D/gralloc_goldfish(1445): Emulator without GPU emulation detected.
02-27 02:02:05.541: D/AndroidRuntime(1445): Shutting down VM
02-27 02:02:05.651: W/dalvikvm(1445): threadid=1: thread exiting with uncaught exception (group=0xb1a7eb90)
02-27 02:02:05.711: E/AndroidRuntime(1445): FATAL EXCEPTION: main
02-27 02:02:05.711: E/AndroidRuntime(1445): Process: andro.androreply, PID: 1445
02-27 02:02:05.711: E/AndroidRuntime(1445): java.lang.IllegalStateException: Could not execute method of the activity
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.view.View$1.onClick(View.java:3814)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.view.View.performClick(View.java:4424)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.view.View$PerformClick.run(View.java:18383)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.os.Handler.handleCallback(Handler.java:733)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.os.Handler.dispatchMessage(Handler.java:95)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.os.Looper.loop(Looper.java:137)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-27 02:02:05.711: E/AndroidRuntime(1445): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 02:02:05.711: E/AndroidRuntime(1445): at java.lang.reflect.Method.invoke(Method.java:515)
02-27 02:02:05.711: E/AndroidRuntime(1445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-27 02:02:05.711: E/AndroidRuntime(1445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-27 02:02:05.711: E/AndroidRuntime(1445): at dalvik.system.NativeStart.main(Native Method)
02-27 02:02:05.711: E/AndroidRuntime(1445): Caused by: java.lang.reflect.InvocationTargetException
02-27 02:02:05.711: E/AndroidRuntime(1445): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 02:02:05.711: E/AndroidRuntime(1445): at java.lang.reflect.Method.invoke(Method.java:515)
02-27 02:02:05.711: E/AndroidRuntime(1445): at android.view.View$1.onClick(View.java:3809)
02-27 02:02:05.711: E/AndroidRuntime(1445): ... 11 more
02-27 02:02:05.711: E/AndroidRuntime(1445): Caused by: java.lang.NullPointerException
02-27 02:02:05.711: E/AndroidRuntime(1445): at andro.androreply.MainActivity.onClick(MainActivity.java:52)
02-27 02:02:05.711: E/AndroidRuntime(1445): ... 14 more
Java代码
import java.io.*;
import java.net.*;
public class Server
{
private static String msg="Hai";
private static Socket client;
private static PrintWriter pw;
private static ServerSocket socket;
public static void main(String args[])
{
try {
socket=new ServerSocket(7777);
client= socket.accept(); //Server socket
pw=new PrintWriter(client.getOutputStream(),true);
pw.write(msg);
pw.flush();
pw.close();
client.close();
}
catch (IOException e)
{
System.out.println("Could not listen on port: 7676");
}
}
}
任何人都可以帮我解决这个问题。提前谢谢......
答案 0 :(得分:1)
您需要instantiate
ac,然后致电execute()
。 execute()
是AsyncTask
的方法。
ac = new AsyncClass();
我看到你有
private AsyncClass ac;
和
public void onClick(View view)
{
ac.execute();
}
但是你没有在任何地方ac
实例化
答案 1 :(得分:0)
你有一个变量AsyncClass ac,但你实际上并没有初始化它。在你的onClick()中,而不是:
ac.execute();
做的:
new AsyncClass.execute();
或首先在onCreate()中初始化 ac 变量:
ac = new AsyncClass();