No clicklisten?

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

No clicklisten?

Postby Gurbuh » Fri Oct 03, 2008 6:07 pm

I am currently trying to develop a working media player, where you can pick songs from a list compiled of songs on the SD card. The list is working ok but nothing happens when i click a song, while i think it should. My SDK version is 1.0. My guess is that the clicklisten is not performing how i want to. Here's my code.


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <AbsoluteLayout
  4.  
  5. android:id="@+id/widget31"
  6.  
  7. android:layout_width="wrap_content"
  8.  
  9. android:layout_height="wrap_content"
  10.  
  11. xmlns:android="http://schemas.android.com/apk/res/android"
  12.  
  13. >
  14.  
  15. <ListView
  16.  
  17. android:id="@+id/list"
  18.  
  19. android:layout_width="wrap_content"
  20.  
  21. android:layout_height="wrap_content"
  22.  
  23. android:layout_x="0px"
  24.  
  25. android:layout_y="0px"
  26.  
  27. >
  28.  
  29. </ListView>
  30.  
  31. </AbsoluteLayout>
  32.  
  33.  
  34.  
  35.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package nl.androidtutorials.punkplayer;
  2.  
  3.  
  4.  
  5. import java.io.File;
  6.  
  7. import java.io.IOException;
  8.  
  9. import java.util.ArrayList;
  10.  
  11.  
  12.  
  13. import android.app.Activity;
  14.  
  15. import android.app.ListActivity;
  16.  
  17. import android.media.MediaPlayer;
  18.  
  19. import android.media.MediaPlayer.OnCompletionListener;
  20.  
  21. import android.os.Bundle;
  22.  
  23. import android.util.Log;
  24.  
  25. import android.view.View;
  26.  
  27. import android.widget.ArrayAdapter;
  28.  
  29. import android.widget.ListView;
  30.  
  31. import android.widget.TextView;
  32.  
  33.  
  34.  
  35. public class player extends ListActivity {
  36.  
  37.         private static final String SD_PATH = new String("/sdcard/");
  38.  
  39.         private MediaPlayer mp = new MediaPlayer();
  40.  
  41.         private ArrayList<String> files = new ArrayList<String>();
  42.  
  43.         public ListView Lijst;
  44.  
  45.         public TextView Info;
  46.  
  47.         public int currentPosition = 0;
  48.  
  49.        
  50.  
  51.     public void onCreate(Bundle savedInstanceState) {
  52.  
  53.         super.onCreate(savedInstanceState);
  54.  
  55.         setContentView(R.layout.main);
  56.  
  57.         Lijst = (ListView)this.findViewById(R.id.list);
  58.  
  59.                 GetSongs();
  60.  
  61.     }
  62.  
  63.    
  64.  
  65.     public void GetSongs() {
  66.  
  67.         File bestand = new File(SD_PATH);
  68.  
  69.             for (File file : bestand.listFiles(new Mp3Filter())) {
  70.  
  71.                 files.add(file.getName());
  72.  
  73.         }
  74.  
  75.        
  76.  
  77.         Lijst.setAdapter(new ArrayAdapter<String>(this,
  78.  
  79.                 android.R.layout.simple_list_item_1, files));
  80.  
  81.     }
  82.  
  83.    
  84.  
  85.     protected void onItemClick(ListView l, View v, int position, long id) {
  86.  
  87.         currentPosition = position;
  88.  
  89.         playSong(SD_PATH + files.get(position));
  90.  
  91.     }
  92.  
  93.    
  94.  
  95.     private void playSong(String songPath) {
  96.  
  97.         try {
  98.  
  99.      
  100.  
  101.             mp.reset();
  102.  
  103.             mp.setDataSource(songPath);
  104.  
  105.             mp.prepare();
  106.  
  107.             mp.start();
  108.  
  109.      
  110.  
  111.             mp.setOnCompletionListener(new OnCompletionListener() {
  112.  
  113.      
  114.  
  115.                 public void onCompletion(MediaPlayer arg0) {
  116.  
  117.                     nextSong();
  118.  
  119.                 }
  120.  
  121.      
  122.  
  123.             });
  124.  
  125.      
  126.  
  127.         } catch (IOException e) {
  128.  
  129.             Log.v(getString(R.string.app_name), e.getMessage());
  130.  
  131.         }
  132.  
  133.     }
  134.  
  135.    
  136.  
  137.     private void nextSong() {
  138.  
  139.         if (++currentPosition >= files.size()) {
  140.  
  141.             currentPosition = 0;
  142.  
  143.         } else {
  144.  
  145.             playSong(SD_PATH + files.get(currentPosition));
  146.  
  147.         }
  148.  
  149.     }
  150.  
  151. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


I hope anyone can help. :(
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Top

Postby MrSnowflake » Sun Oct 05, 2008 12:51 pm

In stead of this: oid onItemClick(ListView l, View v, int position, long id)
use this: void onListItemClick(ListView l, View v, int position, long id)
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Gurbuh » Sun Oct 05, 2008 1:46 pm

Thanks for helping me. But now everytime I run my app it crashes. I seem to get that a lot, with other apps as well. But I never can seem to find the error. I don't understand Eclipse/Android's debug system.
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Postby MrSnowflake » Sun Oct 05, 2008 1:50 pm

If you don't understands eclipses error reporting system, then open cmd.exe and go to androidtools.
Then enter adb logcat.
There you can see all errors the system encountered. Some where near the end of the log you will see familliar java errors, with a stack dump and such you know from desktop java applications. And a bit above the stack dump, you can find the error your app made.

BTW instead of use Lijst for a list, you could use the Hungarian notation, which means, Lijst would become lstMainList, or something like that, so you see the type of the widget by the name alone. Just a hint :).

Natuurlijk is Lijst ook duidelijk, maar als ge 4 Lijsten hebt is het wat minder he :p.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Gurbuh » Tue Oct 07, 2008 10:36 am

Thanks for your reaction, ill look into it.

Maar ben niet van plan meer lijsten te gebruiken dus :P
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Postby Gurbuh » Thu Oct 09, 2008 3:22 pm

There i am again.. I still have an error which is very strange. It says I have to have a ListView in my layout called list, but as you can see above, i have one :shock:
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Top

Postby MrSnowflake » Thu Oct 09, 2008 3:31 pm

Try this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <ListView
  3.  
  4. android:id="@android:id/list"
  5.  
  6. android:layout_width="wrap_content"
  7.  
  8. android:layout_height="wrap_content"
  9.  
  10. android:layout_x="0px"
  11.  
  12. android:layout_y="0px"
  13.  
  14. />
  15.  
  16.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

This is a special case. ListActivity looks for a view with the special android:id/list (or android:list it's in the docs, but could be old, so dunno if that works). you can also add a:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <TextView
  3.  
  4. android:id="@android:id/empty"
  5.  
  6. ...
  7.  
  8. android:text="Deze lijst is leeg" />
  9.  
  10.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

This textview will be showed when the listview is empty, but this is not nescessairy.
Last edited by MrSnowflake on Fri Oct 10, 2008 6:51 pm, edited 1 time in total.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Gurbuh » Fri Oct 10, 2008 6:10 pm

I'm still having the same problem.
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Postby MrSnowflake » Fri Oct 10, 2008 6:49 pm

Did you copy paste my id? and tried both?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Gurbuh » Fri Oct 10, 2008 9:42 pm

MrSnowflake wrote:Did you copy paste my id? and tried both?


I did, but I have to adapt it because it gave me errors. :?
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Postby MrSnowflake » Sat Oct 11, 2008 12:07 pm

I believe I've found it. It was something I overlooked :)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     public void onCreate(Bundle savedInstanceState) {
  3.  
  4.         super.onCreate(savedInstanceState);
  5.  
  6.         setContentView(R.layout.main);
  7.  
  8.         ///////////// NEWµ
  9.  
  10.         Lijst = this.getListView();
  11.  
  12.         ///////////// OLD
  13.  
  14.         //Lijst = (ListView)this.findViewById(R.id.list);
  15.  
  16.           GetSongs();
  17.  
  18.     }
  19.  
  20.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Gurbuh » Sat Oct 11, 2008 1:18 pm

That doesn't seem to work as well. I have however found out more about the problem, so maybe that helps.
When I use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class FinalPlayer extends ListActivity {
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
as my classname the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Lijst = this.getListView();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
does not return an error. But I get the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ljava.lang.RuntimeException: Your content must have a ListView whose id
  2.  
  3.  attribute is 'android.R.id.list'
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
error. My list does not appear now. ,

When I use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class FinalPlayer extends Activity {
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
I do get to see my list (which has 3 items in it.) Trying to use the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Lijst = this.getListView();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
code gives me an error in Eclipse. The listclick listening is not working.
Gurbuh
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Sep 08, 2008 9:49 pm
Location: The Netherlands

Postby MrSnowflake » Sat Oct 11, 2008 8:54 pm

The 1st problem: Are you using the android:id="@android:id/list"? If not do it :), if so, post your current code!
2nd problem: That's normal als getListView() is a method of ListActivity and a ordinary Activity doesn't have a ListView by default, so a getListView() on Activity would be logical and thus there is non :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

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