Aleksandar Herbrich
Aleksandar Herbrich

Reputation: 11

My Android App crashes and gives illigale access exception, i've tried to implement a tcp service to communicate with my server

I am trying to write a service to communicate with a tcp server on www.herbrich.org:2147

But my app keep on crashing every time i run it. Here is my full code from the service 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();
            }

        }

    }
}

I think i've some problem with the multithreading call inside the constructor method. The debug console gives me 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...

Upvotes: 0

Views: 234

Answers (1)

Ahmad Al-Sanie
Ahmad Al-Sanie

Reputation: 3785

your ClientThread class must be public, and has a public zero-argument constructor, and that the constructor chains to the superclass' constructor.

Upvotes: 1

Related Questions