Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!

Jay Pat

Greenhorn
+ Follow
since Jan 16, 2013
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jay Pat

In the ConnectThread inner class the connected() method is called. The connected() method starts the ConnectedThread.
In the Handler and new Intent is created to start the MediaPlayer class.
I am new to Android programming therefore, I'm coming across these difficulties. My aim is to be able to write characters to the OutputStream from a number of classes when buttons are clicked on the Android app.
Do you know of any other methods in which I can achieve this?
5 years ago
I have instantiated the MediaPlayer class in the DeviceListActivity onCreate() method:



I still get a NullPointerException in the MediaPlayer class referring to this line:



The full class is shown in my previous post.
5 years ago
I have now altered the MediaPlayer class and placed the connected() method in this class so that the BluetoothSocket object can be passed to this class:


However, I now get this error:

01-17 10:25:05.498: E/AndroidRuntime(6683): FATAL EXCEPTION: Thread-12
01-17 10:25:05.498: E/AndroidRuntime(6683): java.lang.NullPointerException
01-17 10:25:05.498: E/AndroidRuntime(6683): at com.example.btclient.DeviceListActivity$ConnectThread.run(DeviceListActivity.java:262)

where DeviceListActivity.java:262 refers to this line which is in the ConnectThread inner class:

med is an instance of the MediaPlayer class.
5 years ago
Yes, this may be the solution.
Do you know how I might instantiate the ConnectedThread con since it takes a BluetoothSocket as the parameter?
5 years ago
Apologies for that.
Line 25 refers to this bit of code:
5 years ago
Sorry, I didn't post the correct error message.
I have now edited the error message section.

The handler is within the DeviceListActivity class therefore, it won't compile without the rest of the code in this class. I haven't provided the full code of this class since I don't think it is relevant to my problem.
5 years ago
I am attempting to pass messages from an Android(client) device to my PC(server) however, when the output stream write(bytes) method is called I get a NullPointerException.

The ConnectThread is a inner class, it passes a message to the Handler which then carries out the correct action. A new Intent is created and the new activity begins.

Here is the snippet of code:



Here is the code within the Handler:



The connected() method calls the ConnectedThread passing the socket:



This is the MediaPlayer class. When a button is clicked I want a String or character to be passed to the PC:



This is the ConnectedThread class which I got from the Android developer website. The ConnectedThread is in its own individual class.



The Android and PC establish a connection however, I get this error when I select the 'wmp' button.

01-16 16:46:44.787: E/AndroidRuntime(32682): FATAL EXCEPTION: main
01-16 16:46:44.787: E/AndroidRuntime(32682): java.lang.NullPointerException
01-16 16:46:44.787: E/AndroidRuntime(32682): at com.example.btclient.MediaPlayer$1.onClick(MediaPlayer.java:25)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.view.View.performClick(View.java:2485)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.view.View$PerformClick.run(View.java:9089)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.os.Handler.handleCallback(Handler.java:587)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.os.Looper.loop(Looper.java:130)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.app.ActivityThread.main(ActivityThread.java:3806)
01-16 16:46:44.787: E/AndroidRuntime(32682): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:46:44.787: E/AndroidRuntime(32682): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 16:46:44.787: E/AndroidRuntime(32682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-16 16:46:44.787: E/AndroidRuntime(32682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-16 16:46:44.787: E/AndroidRuntime(32682): at dalvik.system.NativeStart.main(Native Method)
01-16 16:46:44.797: W/ActivityManager(1654): Force finishing activity com.example.btclient/.MediaPlayer


However, the data is sent to the PC if I put the Button onClickListener within the Handler (which is in the same class as the ConnectThread. The code in the Handler looks like this:



But this is not ideal as I would prefer a new activity to begin to handle the Button onClickListeners and pass specific messages to the PC.

5 years ago