wood burning stoves 2.0*
The moose likes Android and the fly likes Having trouble with OnTouchListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Mobile » Android
Bookmark "Having trouble with OnTouchListener" Watch "Having trouble with OnTouchListener" New topic
Author

Having trouble with OnTouchListener

Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Hello! I was hoping somebody could correct my error or help point me in the right direction. I'm trying to create a music application which when you press on the screen in a certain X,Y coordinate, it will play a particular frequency (note).

I have 2 classes; the main activity class, and the other class which is where I'm trying to create my listener. I'm trying to pass the LinearLayout screen to this class called PitchPipe. It crashes upon launching in my emulator. Any help with getting the listener to work would be very much appreciated!

PitchPipe Class


PitchPipeActivity Class


main.xml

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
The first step should be to hook up "adb logcat" to the emulator so you'll see the full exception stack trace.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:The first step should be to hook up "adb logcat" to the emulator so you'll see the full exception stack trace.


I believe this is what you are looking for? I also tried attaching the full log, but i couldn't' find a valid extension.

01-17 17:20:49.465: W/dalvikvm(203): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-17 17:20:49.465: E/AndroidRuntime(203): Uncaught handler: thread main exiting due to uncaught exception
01-17 17:20:49.604: E/AndroidRuntime(203): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sabo.andriod/com.sabo.andriod.PitchPipeActivity}: java.lang.NullPointerException
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.os.Looper.loop(Looper.java:123)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-17 17:20:49.604: E/AndroidRuntime(203): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 17:20:49.604: E/AndroidRuntime(203): at java.lang.reflect.Method.invoke(Method.java:521)
01-17 17:20:49.604: E/AndroidRuntime(203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-17 17:20:49.604: E/AndroidRuntime(203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-17 17:20:49.604: E/AndroidRuntime(203): at dalvik.system.NativeStart.main(Native Method)
01-17 17:20:49.604: E/AndroidRuntime(203): Caused by: java.lang.NullPointerException
01-17 17:20:49.604: E/AndroidRuntime(203): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.view.View.<init>(View.java:1777)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.view.ViewGroup.<init>(ViewGroup.java:279)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.widget.LinearLayout.<init>(LinearLayout.java:88)
01-17 17:20:49.604: E/AndroidRuntime(203): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:17)
01-17 17:20:49.604: E/AndroidRuntime(203): at java.lang.Class.newInstanceImpl(Native Method)
01-17 17:20:49.604: E/AndroidRuntime(203): at java.lang.Class.newInstance(Class.java:1479)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-17 17:20:49.604: E/AndroidRuntime(203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
01-17 17:20:49.604: E/AndroidRuntime(203): ... 11 more
01-17 17:20:49.713: I/Process(52): Sending signal. PID: 203 SIG: 3
01-17 17:20:49.724: I/dalvikvm(203): threadid=7: reacting to signal 3
01-17 17:20:49.724: E/dalvikvm(203): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
01-17 17:20:54.643: I/Process(203): Sending signal. PID: 203 SIG: 9
01-17 17:20:54.673: I/ActivityManager(52): Process com.sabo.andriod (pid 203) has died.
01-17 17:20:54.733: I/UsageStats(52): Unexpected resume of com.android.launcher while already resumed in com.sabo.andriod
01-17 17:20:55.033: W/InputManagerService(52): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44e66810
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What are you trying to achieve by instantiating LinearLayout when you're already declaring it in the main.xml file? It seems that instantiation is what causes the problem.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:What are you trying to achieve by instantiating LinearLayout when you're already declaring it in the main.xml file? It seems that instantiation is what causes the problem.


Well, i'm trying to attach an OnTouchListener to the main screen, but I wasn't sure exactly how to do that. I'm sure my theory is wrong, but I guess that's what I need help with. How would I create a listener and make it monitor touch input on the main "View"? I guess I thought I had to instantiate it in order to attach the listener.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
No, Android does the instantiation. Your code needs to get a reference to it in order to attach the listener. Just about any Android GUI example code shows how to do that in the onCreate handler.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:No, Android does the instantiation. Your code needs to get a reference to it in order to attach the listener. Just about any Android GUI example code shows how to do that in the onCreate handler.


So, something like...



...and then attach the listener to that?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Something like that :-)
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:Something like that :-)


I'll give that a try and reply back with my results. Thanks for guiding me in the right direction!!!
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Mike Sabo wrote:
Tim Moores wrote:Something like that :-)


I'll give that a try and reply back with my results. Thanks for guiding me in the right direction!!!


Ok, so I added the View as a reference, but when I run my application on my simulator, it still crashes. If I remove the code which instantiates the PitchPipe class, the error goes away. So it has something to do with how I'm trying to get my listener to operate in the other class. All of the examples I've found online are created everything within the default class that gets created when you start an Android project. Any ideas on what i'm doing wrong? To me the code looks pretty straight forward.

Modified Code

PitchPipe Class


PitchPipeActivity Class
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I don't think anyone will be able to help unless you post the exact code that causes the problem, and the stack trace.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:I don't think anyone will be able to help unless you post the exact code that causes the problem, and the stack trace.


The line I think I'm having trouble with is line 16 in the PitchPipeActivity Class. The two classes I posted are the only two in my project.

I'm not 100% sure, but I think this is the stack trace...


01-18 17:17:38.669: I/Process(831): Sending signal. PID: 831 SIG: 9
01-18 17:17:49.119: I/dalvikvm(870): Stack overflow, expanding (0x41869200 to 0x41869000)
01-18 17:17:49.140: I/dalvikvm(870): Shrank stack (to 0x41869200, curFrame is 0x4186bcc8)
01-18 17:17:49.149: D/AndroidRuntime(870): Shutting down VM
01-18 17:17:49.159: W/dalvikvm(870): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-18 17:17:49.179: E/AndroidRuntime(870): Uncaught handler: thread main exiting due to uncaught exception
01-18 17:17:49.490: D/dalvikvm(870): GC freed 3646 objects / 333520 bytes in 114ms
01-18 17:17:49.519: E/AndroidRuntime(870): java.lang.StackOverflowError
01-18 17:17:49.519: E/AndroidRuntime(870): at java.util.AbstractList.<init>(AbstractList.java:383)
01-18 17:17:49.519: E/AndroidRuntime(870): at java.util.ArrayList.<init>(ArrayList.java:67)
01-18 17:17:49.519: E/AndroidRuntime(870): at java.util.ArrayList.<init>(ArrayList.java:56)
01-18 17:17:49.519: E/AndroidRuntime(870): at android.app.Activity.<init>(Activity.java:658)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:6)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPipeActivity.java:16)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipe.<init>(PitchPipe.java:12)
01-18 17:17:49.519: E/AndroidRuntime(870): at com.sabo.andriod.PitchPipeActivity.<init>(PitchPi
01-18 17:17:49.609: I/dalvikvm(870): threadid=7: reacting to signal 3
01-18 17:17:49.609: E/dalvikvm(870): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Again: what is "PitchPipe pitchPipe = new PitchPipe()" supposed to do? Your code should not, generally, instantiate UI elements.

In this instance, PitchPipe instantiates PitchPipeActivity, and PitchPipeActivity instantiates PitchPipe - leading to endless recursions that exhausts memory.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:Again: what is "PitchPipe pitchPipe = new PitchPipe()" supposed to do? Your code should not, generally, instantiate UI elements.


I'm not sure, this is where I need the help. I thought I needed to instantiate the class which contains the listener, otherwise, how does the program know the listener exists and what it's assigned to? I know I'm doing it wrong, but I need somebody to tell me what I'm doing wrong.

What I'm trying to achieve:

Have 2 classes. One that has the code for which creates and pushes the screen. The other to contain the listener which controls the touch input.

PitchPipeActivity - From what I understand, this class creates and pushes the screen object.
PitchPipe - controls the touch events.

I instantiated the PitchPipe class so that PitchPipeActivity can see the other class.

It's my first android project ever... I don't know what I'm doing exactly; I'm just going by what I've done in the past from learning Java.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I thought I needed to instantiate the class which contains the listener

No. Like I said, GUI elements are instantiated by Android - your code needs to get a reference to them. Also, you should not put any code in a constructor - all setup stuff should go in the onCreate method. I suggest to read a couple of introductory articles, and to study the code that comes with them; then you should get a better idea of how things work on Android.

These lines attach the listener just fine:

Move those to the onCreate method and delete the constructor.
Mike Sabo
Greenhorn

Joined: Apr 21, 2011
Posts: 19
Tim Moores wrote:
I thought I needed to instantiate the class which contains the listener

No. Like I said, GUI elements are instantiated by Android - your code needs to get a reference to them. Also, you should not put any code in a constructor - all setup stuff should go in the onCreate method. I suggest to read a couple of introductory articles, and to study the code that comes with them; then you should get a better idea of how things work on Android.

These lines attach the listener just fine:

Move those to the onCreate method and delete the constructor.


I apologize for my ignorance, and I thank you for your assistance. I will do as you recommended and read some articles. I'm closing the issue, and I'll revisit it once I've acquired more knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having trouble with OnTouchListener