0
我想寫一個服務與tcp服務器通信www.herbrich.org:2147我的Android應用程序崩潰,並給出illigale訪問異常,我試圖實現一個tcp服務來與我的服務器進行通信
但是我每次運行它時都會崩潰。這是我從服務類完整的代碼。
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.access$1700(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.access$1700(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...
每一個說「崩潰」的問題都應該有一個StackTrace,並指出這次崩潰發生的確切行號是什麼,沒有它,沒有人會幫助你。請編輯你的問題。 – Budius
感謝您的回覆, 你能告訴我你需要什麼,我正在使用android studio。我如何獲得StackTrace? –
發佈LogCat輸出 –