Error during project loading on emulator (media project)

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

Error during project loading on emulator (media project)

Postby Scionwest » Tue Dec 25, 2007 6:13 am

I started using the Playing Media Tutorial found under the Novice section, and modified it a little bit to use the onClickListener differently than that tutorial showed. I found under the Android Docs how to set up a custom listener, but when I execute the application I receive an error within the Android Emulator. Below is both the source code, and the picture of the error message.

mediacenter.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.airheadgaming.android.mediacenter;
  2.  
  3. import android.app.Activity;
  4. import android.content.SharedPreferences;
  5. import android.media.MediaPlayer;
  6. import android.media.MediaPlayer.OnCompletionListener;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11.  
  12. public class mediacenter extends Activity
  13. {
  14.         public static final String PREFS_NAME = "MediaContent";
  15.         public String PREFS_MUSIC = new String("Media\Music");
  16.                
  17.                 //Restore Preferences
  18.                 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  19.    
  20.                 //Widgets
  21.         Button btnPlay = (Button)this.findViewById(R.id.btnPlay);
  22.        
  23.     /** Called when the activity is first created. */
  24.     @Override
  25.     public void onCreate(Bundle icicle)
  26.     {
  27.         super.onCreate(icicle);
  28.         btnPlay.setOnClickListener(btnPlay_Click);
  29.  
  30.         setContentView(R.layout.main);
  31.     }
  32.    
  33.     public OnClickListener btnPlay_Click = new OnClickListener()
  34.     {
  35.         public void onClick(View v)
  36.         {
  37.                 MediaPlayer mp = MediaPlayer.create(mediacenter.this, R.raw.sample);
  38.             mp.prepare();
  39.             mp.start();
  40.         }
  41.     };
  42. }
  43.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4




main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    android:background="#ff000000"  
  7. >    
  8.     <TextView      
  9.     id="@+id/mediacenterbanner"
  10.     android:textColor="#ffffffff"
  11.     android:textSize="12dip"
  12.          android:layout_width="fill_parent"
  13.          android:layout_height="wrap_content"
  14.          android:textAlign="center"
  15.          android:text="Media Center Home"
  16.          android:layout_marginTop="5dip"
  17.          android:textStyle="bold"
  18.          android:layout_marginBottom="5dip"
  19.     />  
  20.      
  21.      <ImageSwitcher id="@+id/switcher"
  22.        android:layout_width="fill_parent"
  23.        android:layout_height="fill_parent"                
  24.        android:layout_alignParentLeft="true"
  25.          android:layout_alignParentRight="true"
  26.          android:layout_alignParentBottom="true"      
  27.          android:layout_below="@id/mediacenterbanner"
  28.          android:text="Albums"
  29.    />
  30.      <Gallery id="@+id/gallery"
  31.        android:background="#55000000"
  32.        android:layout_width="fill_parent"
  33.        android:layout_height="60dip"        
  34.        android:layout_alignParentLeft="true"
  35.          android:layout_alignParentRight="true"
  36.        android:gravity="center_vertical"              
  37.        android:spacing="16"
  38.        android:layout_above="@+id/btnPlay"
  39.    />
  40.     <Button
  41.     id="@+id/btnPlay"      
  42.     android:text="Play"
  43.     android:textSize="12dip"
  44.     android:layout_width="wrap_content"
  45.          android:layout_height="wrap_content"
  46.          android:layout_alignParentBottom="true"
  47.          android:layout_alignParentLeft="true"
  48.          android:textAlign="center"          
  49.    />
  50. </RelativeLayout>
  51.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


Error
Image

Any suggestions? :?:
Scionwest
Freshman
Freshman
 
Posts: 5
Joined: Tue Dec 25, 2007 6:04 am

Top

Postby Scionwest » Tue Dec 25, 2007 6:33 am

I have narrowed it down to the btnPlay.setOnClickListener that is causing the error. Below is my updated code. As both the SharedPreferences and the btnPlay declarations where causing errors as well. I'm assuming it has something to do with the fact that it's being declared outside of the onCreate() method, as once I moved the btnPlay declaration into that method it's error went away.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.airheadgaming.android.mediacenter;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.SharedPreferences;
  8.  
  9. import android.media.MediaPlayer;
  10.  
  11. import android.media.MediaPlayer.OnCompletionListener;
  12.  
  13. import android.os.Bundle;
  14.  
  15. import android.view.View;
  16.  
  17. import android.view.View.OnClickListener;
  18.  
  19. import android.widget.Button;
  20.  
  21.  
  22.  
  23. public class mediacenter extends Activity
  24.  
  25. {
  26.  
  27.         public static final String PREFS_NAME = "MediaContent";
  28.  
  29.         public String PREFS_MUSIC = new String("Media\\Music");
  30.  
  31.     //Restore Preferences
  32.  
  33.     //SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  34.  
  35.        
  36.  
  37.     /** Called when the activity is first created. */
  38.  
  39.     @Override
  40.  
  41.     public void onCreate(Bundle icicle)
  42.  
  43.     {
  44.  
  45.         super.onCreate(icicle);
  46.  
  47.       //Widgets
  48.  
  49.         Button btnPlay = (Button)this.findViewById(R.id.btnPlay);
  50.  
  51.        
  52.  
  53.      //   btnPlay.setOnClickListener(btnPlay_Click);
  54.  
  55.    
  56.  
  57.         setContentView(R.layout.main);
  58.  
  59.     }
  60.  
  61.    
  62.  
  63.     public OnClickListener btnPlay_Click = new OnClickListener()
  64.  
  65.     {
  66.  
  67.         public void onClick(View v)
  68.  
  69.         {
  70.  
  71.                 try {                                                                  
  72.  
  73.  
  74.  
  75.                         MediaPlayer mp = MediaPlayer.create(mediacenter.this, R.raw.sample);
  76.  
  77.                 mp.prepare();
  78.  
  79.                 mp.start();
  80.  
  81.            }
  82.  
  83.            catch (Exception e) {  
  84.  
  85.            }
  86.  
  87.         }
  88.  
  89.     };
  90.  
  91. }
  92.  
  93.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Scionwest
Freshman
Freshman
 
Posts: 5
Joined: Tue Dec 25, 2007 6:04 am

Postby plusminus » Wed Dec 26, 2007 8:59 pm

Hello Scionwest,

The "SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);" caused the error with me. Also I think (almost sure), that you cannot use findViewById(...) before you called setContentView(...), as findViewById(...) relies on setContentView(...). So you cannot use findViewById(...) on class-fields. This works with me (except the SharedPreferences):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.musicplayer;
  2.  
  3. import android.app.Activity;
  4. import android.content.SharedPreferences;
  5. import android.media.MediaPlayer;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10.  
  11. public class MusicPlayer extends Activity {
  12.  
  13.         public static final String PREFS_NAME = "MediaContent";
  14.         public String PREFS_MUSIC = new String("Media\Music");
  15.  
  16.         //Restore Preferences
  17. //      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  18.  
  19.         //Widgets
  20.         Button btnPlay = null;
  21.  
  22.         /** Called when the activity is first created. */
  23.         @Override
  24.         public void onCreate(Bundle icicle) {
  25.                 super.onCreate(icicle);
  26.                
  27.                 setContentView(R.layout.main);
  28.                 btnPlay = (Button) this.findViewById(R.id.btnPlay);
  29.                 btnPlay.setOnClickListener(btnPlay_Click);
  30.         }
  31.  
  32.         public OnClickListener btnPlay_Click = new OnClickListener() {
  33.  
  34.                 public void onClick(View v) {
  35.                         MediaPlayer mp = MediaPlayer.create(MusicPlayer.this,
  36.                                         R.raw.everlast);
  37.                         mp.prepare();
  38.                         mp.start();
  39.                 }
  40.         };
  41. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

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