我的 Android 应用程序崩溃并出现非法访问异常,我尝试实现一个 tcp 服务来与我的服务器通信
My Android App crashes and gives illigale access exception, i've tried to implement a tcp service to communicate with my server
我正在尝试编写一个服务来与 www.herbrich.org:2147
上的 TCP 服务器通信
但是我的应用程序每次 运行 都会崩溃。这是我来自服务 class.
的完整代码
ServiceClass
:
package org.herbrich.katana;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Random;
/**
* Created by Administrator on 16.04.2015.
*/
public class LocalService extends Service{
LocalService()
{
new Thread(new ClientThread()).start();
}
//JenniferHerbrich Network Ansi Declares
private Socket socket;
private static final int SERVERPORT;
private static final String SERVER_IP;
static {
SERVER_IP = "10.141.0.151";
SERVERPORT = 2147;
}
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public method
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
public String communicateWithEvelin(String EvelinValue)
{
try
{
BufferedReader input;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(EvelinValue);
out.flush();
input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
return input.readLine();
}
catch(Exception e)
{
return "ERROR";
}
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
我想我在构造方法中的多线程调用有一些问题。调试控制台给我 illegalAccessExceptions
..
LogCat:
04-15 18:52:08.632 9182-9182/org.herbrich.katana E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service org.herbrich.katana.LocalService: java.lang.IllegalAccessException: access to constructor not allowed
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2599)
at android.app.ActivityThread.access00(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalAccessException: access to constructor not allowed
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2596)
at android.app.ActivityThread.access00(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
04-15 18:52:08.640 9182-9197/org.herbrich.katana E/TCP Client﹕ C: Connecting...
你的 ClientThread
class 必须是 public,并且有一个 public 零参数构造函数,并且构造函数链接到 superclass'
构造函数.
我正在尝试编写一个服务来与 www.herbrich.org:2147
上的 TCP 服务器通信但是我的应用程序每次 运行 都会崩溃。这是我来自服务 class.
的完整代码ServiceClass
:
package org.herbrich.katana;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Random;
/**
* Created by Administrator on 16.04.2015.
*/
public class LocalService extends Service{
LocalService()
{
new Thread(new ClientThread()).start();
}
//JenniferHerbrich Network Ansi Declares
private Socket socket;
private static final int SERVERPORT;
private static final String SERVER_IP;
static {
SERVER_IP = "10.141.0.151";
SERVERPORT = 2147;
}
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public method
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
public String communicateWithEvelin(String EvelinValue)
{
try
{
BufferedReader input;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(EvelinValue);
out.flush();
input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
return input.readLine();
}
catch(Exception e)
{
return "ERROR";
}
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
我想我在构造方法中的多线程调用有一些问题。调试控制台给我 illegalAccessExceptions
..
LogCat:
04-15 18:52:08.632 9182-9182/org.herbrich.katana E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service org.herbrich.katana.LocalService: java.lang.IllegalAccessException: access to constructor not allowed
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2599)
at android.app.ActivityThread.access00(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalAccessException: access to constructor not allowed
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2596)
at android.app.ActivityThread.access00(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
04-15 18:52:08.640 9182-9197/org.herbrich.katana E/TCP Client﹕ C: Connecting...
你的 ClientThread
class 必须是 public,并且有一个 public 零参数构造函数,并且构造函数链接到 superclass'
构造函数.