Error while starting/binding service from sherlock fragment in android

210 Views Asked by At

I'm trying to start and blind service from a sherlock fragment class and for some reason the app is simply crashing while starting/binding the service! The same code worked for me without fragment! I also tried adding a full package name for the service in the manifest file but still not worked!

public class Client extends SherlockFragment implements Interface,
        OnClickListener {

    Button play;
    TextView title;

    // Bass Service
    private Server background = null;

    // Bass Service Connection
    private ServiceConnection bgLink = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder service) {
            background = ((Server.ServerBinder) service).getService();
            onServerConnected();
        }

        public void onServiceDisconnected(ComponentName name) {
            background = null;
        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_player, container,
                false);

        play = (Button) rootView.findViewById(R.id.btPlay);
        play.setOnClickListener(this);
        title = (TextView) rootView.findViewById(R.id.radio_status);

        // Start Service
        getActivity().startService(new Intent(getActivity(),Server.class));

        // Bind Service
        getActivity().bindService(new Intent(getActivity(), Server.class),
                bgLink, 1);

        return rootView;
    }

    @Override
    public void onDestroy() {
        background.unsetActivity();

        // Unbind Service
        getActivity().unbindService(bgLink);

        super.onDestroy();
    }

    // onServerConnected: Put some activity stuff here
    public void onServerConnected() {

        // Register Activity
        background.setActivity(this);
    }

    public void switchPlaying() {
        if (background.getStatus() != 2) {
            if (background.getStatus() == 1) { // Playing
                background.Stop();
                statusUpdate(getString(R.string.welcome));
            } else { // Idle
                statusUpdate(getString(R.string.connecting));
                background.Play();
            }
        }
    }


    public void msg(String text, int duration) {
        Toast.makeText(getActivity(), text, duration).show();
    }

    public void statusUpdate(String text) {
        title.setText(text);
    }

    @Override
    public void onNewTrack(final String meta) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                statusUpdate(meta);
            }
        });
    }

    @Override
    public void onStoppedPlayback() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                statusUpdate(getString(R.string.welcome));
            };
        });
    }

    @Override
    public void onError(final String msg) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                msg(msg, 1);
            }
        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btPlay: {
            switchPlaying();
        }
        }
    }
}

Log Cat output:

12-01 05:28:16.155: E/AndroidRuntime(2747): FATAL EXCEPTION: main
12-01 05:28:16.155: E/AndroidRuntime(2747): Process: com.inspiron.app, PID: 2747
12-01 05:28:16.155: E/AndroidRuntime(2747): java.lang.UnsatisfiedLinkError: Couldn't load bass from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.inspiron.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.inspiron.app-1, /system/lib]]]: findLibrary returned null
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.Runtime.loadLibrary(Runtime.java:358)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.System.loadLibrary(System.java:526)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.un4seen.bass.BASS.<clinit>(BASS.java:675)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.inspiron.app.Server.onCreate(Server.java:58)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread.access$1700(ActivityThread.java:135)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.os.Looper.loop(Looper.java:137)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invoke(Method.java:515)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at dalvik.system.NativeStart.main(Native Method)
1

There are 1 best solutions below

0
On

Actually I don't know what's wrong in your code but I am always use the concept (Inter-process communication with Messenger) like this

 private Messenger messenger;
private ServiceConnection conn = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        messenger = new Messenger(binder);
    }

    public void onServiceDisconnected(ComponentName className) {
        messenger = null;
    }
};

and the onBind method of service should be like this

@Override
public IBinder onBind(Intent arg0) {
    Bundle extras = arg0.getExtras();
    if (extras != null) {
        outMessenger = (Messenger) extras.get("MESSENGER");
    }
    return inMessenger.getBinder();
}

you should use this approach with Handler

// In the activity
private Handler myHandler = new Handler() {

    public void handleMessage(android.os.Message msg) {
    }
}

// In the service
private class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
     }
 }

Hope be usefull