Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Android
unable to create the service ,it gives null pointer exception
shawn peter
Ranch Hand
Posts: 1325
1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
here is my main class
package intent.filters; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class IntentExamples extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button Activity1 = (Button) findViewById(R.id.Button01); Activity1.setOnClickListener(this); Button startButton = (Button) findViewById(R.id.startButton); startButton.setOnClickListener(this); Button stopButton = (Button) findViewById(R.id.stopButton); stopButton.setOnClickListener(this); } @Override public void onClick(View v) { //Intent myIntent =new Intent(v.getContext(), DigitalClock.class); ///startActivityForResult(myIntent, 0); if(v.getId()==R.id.Button01){ Intent myIntent =new Intent(this, DigitalClock.class); startActivity(myIntent); } if(v.getId()==R.id.startButton){ startService(new Intent(getBaseContext(), MediaPlayerService.class)); } if(v.getId()==R.id.stopButton){ stopService(new Intent(getBaseContext(), MediaPlayerService.class)); } } }
here is service class
package intent.filters; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MediaPlayerService extends Service { MediaPlayer myMediaPlayer; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { myMediaPlayer = MediaPlayer.create(this, R.raw.mindtaffy); myMediaPlayer.setLooping(true); } @Override public void onStart(Intent intent, int startid) { myMediaPlayer.start(); } @Override public void onDestroy() { myMediaPlayer.stop(); } }
here is manifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="intent.filters" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="3" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".IntentExamples" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DigitalClock"></activity> <service android:enabled="true" android:name=".MediaPlayerService" /> </application> </manifest>
here is exception in logcat
07-21 18:22:28.880: WARN/dalvikvm(573): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): FATAL EXCEPTION: main 07-21 18:22:28.900: ERROR/AndroidRuntime(573): java.lang.RuntimeException: Unable to create service intent.filters.MediaPlayerService: java.lang.NullPointerException 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2969) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.app.ActivityThread.access$3300(ActivityThread.java:125) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.os.Handler.dispatchMessage(Handler.java:99) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.os.Looper.loop(Looper.java:123) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.app.ActivityThread.main(ActivityThread.java:4627) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at java.lang.reflect.Method.invokeNative(Native Method) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at java.lang.reflect.Method.invoke(Method.java:521) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at dalvik.system.NativeStart.main(Native Method) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): Caused by: java.lang.NullPointerException 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at intent.filters.MediaPlayerService.onCreate(MediaPlayerService.java:21) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959) 07-21 18:22:28.900: ERROR/AndroidRuntime(573): ... 10 more 07-21 18:22:28.940: WARN/ActivityManager(58): Force finishing activity intent.filters/.IntentExamples 07-21 18:22:29.490: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44f9b5c8 intent.filters/.IntentExamples}
andrew chou
Greenhorn
Posts: 1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
guess u'r
testing
it on Virtual Machine.
add the following part to the onCreate of your Service,you'll understand:
if(player == null) System.out.println(" MediaPlayer not surported here ");
Good luck!
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
getting null pointer
is there any way to trigger onLocationChanged other than moving
contacts doesn't display
error in fragment example
How to find out programatically that file is already exist or not?
More...