Debugging help

Put your problem here if it does not fit any of the other categories.

Debugging help

Postby CyberWalrus » Sat Oct 24, 2009 1:06 pm

Hi all, im very new to programming for android, only started yesterday :P
I have been developing a little app for fun using bits and pieces from tuts found here and other places.
The idea is it fetches .lrc automaticaly from a database and syncs them with the music that you are playing, eventualy in a nice widget on the homescreen. Right now, i have done the code for retreiving the correct lyrics off the database, but when i run it on my hero, it force closes immidiately. I figure there must be loads of bugs in it, but since i realy dont know anything about java, i dont have a clue how to debug it :P Heres my code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.walrus.lyric;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import java.io.BufferedInputStream;
  8.  
  9. import java.io.InputStream;
  10.  
  11. import java.net.URL;
  12.  
  13. import java.net.URLConnection;
  14.  
  15.  
  16.  
  17.  
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22.  
  23. import android.app.Activity;
  24.  
  25. import android.os.Bundle;
  26.  
  27. import android.widget.TextView;
  28.  
  29.  
  30.  
  31. public class lyricviewer extends Activity {
  32.  
  33.      @Override
  34.  
  35.      public void onCreate(Bundle icicle) {
  36.  
  37.           super.onCreate(icicle);
  38.  
  39.  
  40.  
  41.           /* We will show the data we read in a TextView. */
  42.  
  43.           TextView tv = new TextView(this);
  44.  
  45.          
  46.  
  47.           /* Will be filled and displayed later. */
  48.  
  49.           String data = null;
  50.  
  51.           String track = "night";
  52.  
  53.           String id = null;
  54.  
  55.           String lyrics = null;
  56.  
  57.           try {
  58.  
  59.          
  60.  
  61.  
  62.  
  63.                /* Define the URLs we want to load data from. */
  64.  
  65.                URL searchURL = new URL(
  66.  
  67.                          "http://www.lyrdb.com/lookup.php?q="+track+"&for=trackname&agent=andoridlyricviewer");
  68.  
  69.                /* Open a connection to that URL. */
  70.  
  71.                URLConnection ucon = searchURL.openConnection();
  72.  
  73.  
  74.  
  75.                /* Define InputStreams to read
  76.  
  77.                 * from the URLConnection. */
  78.  
  79.                InputStream is = ucon.getInputStream();
  80.  
  81.                BufferedInputStream bis = new BufferedInputStream(is);
  82.  
  83.                
  84.  
  85.                /* Read bytes to the Buffer until
  86.  
  87.                 * there is nothing more to read(-1). */
  88.  
  89.                ByteArrayBuffer baf = new ByteArrayBuffer(50);
  90.  
  91.                int current = 0;
  92.  
  93.                while((current = bis.read()) != -1){
  94.  
  95.                     baf.append((byte)current);
  96.  
  97.                }
  98.  
  99.  
  100.  
  101.                /* Convert the Bytes read to a String. */
  102.  
  103.                data = new String(baf.toByteArray());
  104.  
  105.           } catch (Exception e) {
  106.  
  107.                /* On any Error we want to display it. */
  108.  
  109.                data = e.getMessage();                
  110.  
  111.           }
  112.  
  113.           /* find the id of the lyrics */
  114.  
  115.                        int num = data.indexOf("/") ;
  116.  
  117.                        id = data.substring(0, num);
  118.  
  119.               try {
  120.  
  121.                            
  122.  
  123.  
  124.  
  125.                        /* Define the URLs we want to load data from. */
  126.  
  127.                        URL lyricURL = new URL(
  128.  
  129.                                  "http://www.lyrdb.com/getlyr.php?q="+id);
  130.  
  131.                        /* Open a connection to that URL. */
  132.  
  133.                        URLConnection ucon = lyricURL.openConnection();
  134.  
  135.  
  136.  
  137.                        /* Define InputStreams to read
  138.  
  139.                         * from the URLConnection. */
  140.  
  141.                        InputStream is = ucon.getInputStream();
  142.  
  143.                        BufferedInputStream bis = new BufferedInputStream(is);
  144.  
  145.                        
  146.  
  147.                        /* Read bytes to the Buffer until
  148.  
  149.                         * there is nothing more to read(-1). */
  150.  
  151.                        ByteArrayBuffer baf = new ByteArrayBuffer(50);
  152.  
  153.                        int current = 0;
  154.  
  155.                        while((current = bis.read()) != -1){
  156.  
  157.                             baf.append((byte)current);
  158.  
  159.                        }
  160.  
  161.  
  162.  
  163.                        /* Convert the Bytes read to a String. */
  164.  
  165.                        lyrics = new String(baf.toByteArray());
  166.  
  167.                   } catch (Exception e) {
  168.  
  169.                        /* On any Error we want to display it. */
  170.  
  171.                        lyrics = e.getMessage();                
  172.  
  173.                   }
  174.  
  175.                 /* Put the lyrics data in the text field */  
  176.  
  177.            tv.setText(lyrics);
  178.  
  179.            this.setContentView(tv);
  180.  
  181.           }
  182.  
  183. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4

Thanks a lot for any help anyone can give me.
PS. If anyone knows how to get the android music players "now playing" info, that would be appriciated as well :D
CyberWalrus
Freshman
Freshman
 
Posts: 2
Joined: Sat Oct 24, 2009 12:50 pm
Location: England

Top

Postby wiederke » Sat Oct 24, 2009 2:20 pm

Hi,

you should first check the output of Logcat. There you will find the Exception causing your application to close.


Bye,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby CyberWalrus » Sat Oct 24, 2009 5:33 pm

wiederke wrote:Hi,

you should first check the output of Logcat. There you will find the Exception causing your application to close.


Bye,

Torsten

Thanks a lot, i sifted through logcats output and i think i found all the relevent bits, but i dont know what any of it means... Could anyone explain what this is trying to tell me
Code: Select all
{com.walrus.lyric/com.walrus.lyric.lyricviewer} }
I/ActivityManager(  138): Start proc com.walrus.lyric for activity com.walrus.lyric/.lyricviewer: pid=1036 uid=10145 gids={3003}
I/WindowManager(  138): Screen status=true, current orientation=-1, SensorEnabled=false
I/WindowManager(  138): needSensorRunningLp, mCurrentAppOrientation =-1
D/Sensors (  138): open_akm, fd=106
D/Sensors (  138): sensors=00000001, real=00000001
D/dalvikvm(   99): GC freed 268 objects / 10136 bytes in 177ms
D/Sensors (  138): using /dev/input/event4 (name=compass)
D/Sensors (  138): data__data_open: fd = 107
I/WindowManager(  138): Enabling listeners
D/InetAddress( 1036): www.lyrdb.com: 78.110.167.9 (family 2, proto 6)
D/InetAddress( 1036): webservices.lyrdb.com: 78.110.167.9 (family 2, proto 6)
I/global  ( 1036): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
D/AndroidRuntime( 1036): Shutting down VM
W/dalvikvm( 1036): threadid=3: thread exiting with uncaught exception (group=0x40013140)
E/AndroidRuntime( 1036): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.walrus.lyric/com.walrus.lyric.lyricviewer}: java.lang.StringIndexOutOfBoundsException
E/AndroidRuntime( 1036):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
E/AndroidRuntime( 1036):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
E/AndroidRuntime( 1036):    at android.app.ActivityThread.access$1800(ActivityThread.java:112)
E/AndroidRuntime( 1036):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
E/AndroidRuntime( 1036):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1036):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1036):    at android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime( 1036):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1036):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1036):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime( 1036):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime( 1036):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1036): Caused by: java.lang.StringIndexOutOfBoundsException
E/AndroidRuntime( 1036):    at java.lang.String.substring(String.java:1571)
E/AndroidRuntime( 1036):    at com.walrus.lyric.lyricviewer.onCreate(lyricviewer.java:58)
E/AndroidRuntime( 1036):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
E/AndroidRuntime( 1036):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
E/AndroidRuntime( 1036):    ... 11 more
I/Process (  138): Sending signal. PID: 1036 SIG: 3
I/dalvikvm( 1036): threadid=7: reacting to signal 3
I/ActivityManager(  138): Process com.walrus.lyric (pid 1036) has died
CyberWalrus
Freshman
Freshman
 
Posts: 2
Joined: Sat Oct 24, 2009 12:50 pm
Location: England

Postby wiederke » Sun Oct 25, 2009 9:21 am

Hi,

the important information is:

E/AndroidRuntime( 1036): at com.walrus.lyric.lyricviewer.onCreate(lyricviewer.java:58 )

Check the code in line of your lyricviewer class. You have a problem with id = data.substring(0, num);. May be num is -1 because the String data doesn't contain as slash (/). I suggest printing the variable data and num to the log (android.util.Log.d(String tag, String msg)) to check their values.


Bye

Torsten

PS: Your class lyricviewer should start with a upper case letter, because of the Java naming convention.
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests