Android中的java.lang.IllegalStateException
我尝试使用套接字连接从java桌面程序向android发送字符串。当用户单击android应用程序中的按钮时,它将显示来自java应用程序的消息。当我点击按钮时,它会显示java.lang.IllegalStateException。我可以在这个网站上找到很多这样的问题,但没有一个不符合我的要求 Android代码Android中的java.lang.IllegalStateException,java,android,sockets,Java,Android,Sockets,我尝试使用套接字连接从java桌面程序向android发送字符串。当用户单击android应用程序中的按钮时,它将显示来自java应用程序的消息。当我点击按钮时,它会显示java.lang.IllegalStateException。我可以在这个网站上找到很多这样的问题,但没有一个不符合我的要求 Android代码 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader
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;
}
}
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");
}
}
}
Java代码
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;
}
}
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");
}
}
}
任何人都可以帮我解决这个问题。提前感谢…您需要
实例化
ac,然后调用execute()
execute()
是AsyncTask
的方法
ac = new AsyncClass();
我看你有
private AsyncClass ac;
及
但是您没有在任何地方实例化
ac
ac您有一个变量AsyncClass ac,但实际上并没有初始化它。在onClick()中,而不是:
ac.execute();
做:
或者首先在onCreate()中使用以下命令初始化ac变量:
ac = new AsyncClass();
将您的
hardtask()
代码放入AsyncTask InBackground(..)
在andro.androreply.MainActivity.onClick(MainActivity.java:52)收到的空指针异常在你的主要活动的52行上。检查你在哪里做了mistake@MDhardtask
在doInbackground
中调用。是的,我忘记实例化ac对象。在我实例化ac之后,我也无法在我的logcat中找到名为message的标记。谢谢,我得到了答案。
ac = new AsyncClass();