简体   繁体   中英

java.lang.IllegalStateException In Android

I try to send a string from the java desktop program to android using socket connection. When the user click a button in the android application it will displays the message from the java app. When i click the Button it displays java.lang.IllegalStateException.I can found many questions like this in this site but noone dont match my requirement.

Android Code

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 Code

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");
    }
  }
} 

Anyone can help me to resolve this. Thanks in advance...

You need to instantiate ac and then call execute() . execute() is method of AsyncTask .

ac = new AsyncClass();

I see you have

private AsyncClass ac;

And

public void onClick(View view)
{
ac.execute();
}

But you have not instantiated ac anywhere

You have a variable AsyncClass ac, but you're not actually initialising it. In your onClick(), instead of:

ac.execute();

do:

new AsyncClass.execute();

or initialise your ac variable in onCreate() first with:

ac = new AsyncClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM