java - getApplicationContext() method getting error -
i android beginner, making simple service
example.
but in single code getapplicationcontext()
method behaves differently, please check comment of mainactivity
.
mainactivity.java
package com.avisingh.servicetest; import android.content.dialoginterface; import android.content.intent; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final button alertbutton = (button)findviewbyid(r.id.alert_btn); alertbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { alertdialog.builder builder = new alertdialog.builder(mainactivity.this); //if placing here getapplicationcontext() method getting error; builder.settitle("warning"); builder.setmessage("are sure open media played in app?"); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { startactivity(new intent(getapplicationcontext(),playeractivity.class)); // here getapplicationcontext() , mainactivity.this both working } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog alertdialog = builder.create(); alertdialog.show(); //getting error here. } }); } }
playeractivity.java
package com.avisingh.servicetest; import android.content.intent; import android.os.bundle; import android.support.annotation.nullable; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; public class playeractivity extends appcompatactivity{ @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.player_activity); final button startbtn = (button)findviewbyid(r.id.start_btn); final button stopbtn = (button)findviewbyid(r.id.stop_btn); startbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startservice(new intent(getapplicationcontext(),myservices.class)); } }); stopbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { stopservice(new intent(getapplicationcontext(),myservices.class)); } }); } }
myservices.java
package com.avisingh.servicetest; import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.os.ibinder; import android.support.annotation.nullable; public class myservices extends service { mediaplayer mediaplayer; @nullable @override public ibinder onbind(intent intent) { //return null; throw new unsupportedoperationexception("not implemented"); } @override public void oncreate() { super.oncreate(); mediaplayer = mediaplayer.create(getapplicationcontext(),r.raw.one_man); } @override public int onstartcommand(intent intent, int flags, int startid) { mediaplayer.start(); return super.onstartcommand(intent, flags, startid); } @override public void ondestroy() { super.ondestroy(); mediaplayer.stop(); } }
androidmanifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.avisingh.servicetest"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".playeractivity"/> <service android:name=".myservices"/> </application>
activity_main
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.avisingh.servicetest.mainactivity"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/alert_btn" android:layout_centerhorizontal="true" android:layout_margintop="40dp" android:id="@+id/alert_btn"/> </relativelayout>
player_activity
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:text="@string/start_btn" android:id="@+id/start_btn"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="50dp" android:text="@string/stop_btn" android:id="@+id/stop_btn"/> </relativelayout>
error:
08-14 14:17:58.916 6874-6874/com.avisingh.servicetest e/androidruntime: fatal exception: main process: com.avisingh.servicetest, pid: 6874 java.lang.illegalstateexception: need use theme.appcompat theme (or descendant) activity. @ android.support.v7.app.appcompatdelegateimplv9.createsubdecor(appcompatdelegateimplv9.java:359) @ android.support.v7.app.appcompatdelegateimplv9.ensuresubdecor(appcompatdelegateimplv9.java:328) @ android.support.v7.app.appcompatdelegateimplv9.setcontentview(appcompatdelegateimplv9.java:289) @ android.support.v7.app.appcompatdialog.setcontentview(appcompatdialog.java:83) @ android.support.v7.app.alertcontroller.installcontent(alertcontroller.java:225) @ android.support.v7.app.alertdialog.oncreate(alertdialog.java:257) @ android.app.dialog.dispatchoncreate(dialog.java:373) @ android.app.dialog.show(dialog.java:274) @ com.avisingh.servicetest.mainactivity$1.onclick(mainactivity.java:42) @ android.view.view.performclick(view.java:4759) @ android.view.view$performclick.run(view.java:19770) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5247) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:912) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:707)
//if placing here getapplicationcontext() method getting error
correct. because application
object not know themes, , dialog needs theme based on theme.appcompat
. use activity
context creating dialogs.
you may wish read dave smith's awesome blog post on uses of different types of context
object more on this.
Comments
Post a Comment