I am also having a hard time figuring out how the Handler compiles. There seems to be some excess brackets happening someplace. When I try to align open and close braces/parenthesis I get this:

Steve

Jay Pat

Greenhorn

Posts: 8

posted 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.

You have to provide enough context to answer your question. No code you posted shows med.connected(mmSocket, mmDevice); No code show you declare or assign to med. We can't answer these questions without context. Here is what we do know:
med.connected(mmSocket, mmDevice); Throws a NullPointerException. Therefore:

med is an instance of the MediaPlayer class.

This statement is probably false. What we can guess is that med is a variable of type MediaPlayer which is null. You need to either instantiate it or pass a value to an actual instance to it.

Steve

Jay Pat

Greenhorn

Posts: 8

posted 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:

I think you are running in circles, sit back and think about your design and the dependencies.

In your first post you are starting the MediaPlayer using an Intent. Somewhere (not shown) you are calling med = new MediaPlayer(). On one of those objects you are calling connected(...). And one of those two instances of MediaPlayer is becoming the OnClickListener - probably the one that gets created from the Intent because it would go through the Activity lifecycle and the onCreate() would be called.

You need to simplify things. Figure out what classes you need, when you need them, and how you pass things from one to the other. What you are doing now is not working. The attempts to fix it are shots in the dark. You need to stop coding and start planning the data flow.

Steve

Jay Pat

Greenhorn

Posts: 8

posted 5 years ago

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?

I don't know, nor expect you to post, the full specification so it would be hard come up with something that will work in your specific case. I suggest you take a pencil and paper and draw up your application's work flow. Then, for each step determine what data is required for that step to work. Then determine where that data comes from. Then determine how the data can get from the source to where it is used.

The normal way to pass data from one Activity to the next is to use the overloaded Intent#setExtra(String name, ...) methods. If you can pass around basic data types or Parcelable objects, this is the route to go. But you should be careful about the data types you are passing around, since you are limited to the types of data (and for normal good encapsulation). For example do you have to pass around the Bluetooth sockets? Or just the name/id of the device to connect to? Do you really have to pass around the 'ConnectionThread'? What is it's purpose? Is it a Thread or a data holder? It should not be both (it should never be a Thread at all in my opinion, but that is a different discussion). If you can separate the data from the task then you could much easier pass the data around.

Steve

Jay Pat

Greenhorn

Posts: 8

posted 5 years ago

Ok, thanks for your help

machines help you to do more, but experience less. Experience this tiny ad: