Error Message
11:32:37.946 16933-16933/comps411.tma04.trafficcounter W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x415a7ba8)
01-02 11:32:37.956 16933-16933/comps411.tma04.trafficcounter E/AndroidRuntime: FATAL EXCEPTION: main
Process: comps411.tma04.trafficcounter, PID: 16933
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
at android.support.v7.app.AlertController.installContent(AlertController.java:213)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
at android.app.Dialog.show(Dialog.java:262)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:902)
at comps411.tma04.trafficcounter.CountingActivity.onOptionsItemSelected(CountingActivity.java:120)
at android.app.Activity.onMenuItemSelected(Activity.java:2600)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:68)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:172)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
at android.widget.AbsListView$3.run(AbsListView.java:3645)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Source code
Finally found the problem is not much related to the error message: "You need to use a Theme.AppCompat theme (or descendant) with this activity". My case isn't related to adding sth like android:theme="@style/Theme.AppCompat in AndroidManifest.xml suggested from internet exiting case answer.Do not use getApplicationContext() to get application context for AlertDialog constructor, use this keyword to use activity context :
new AlertDialog.Builder(getApplicationContext())
.setTitle("Developer")
.setMessage("Hello")
.show();
use "this" instead:
new AlertDialog.Builder(this)
.setTitle("Developer")
.setMessage("Hello")
.show();
For further reading, you can click to read the different between application context and activity context: What is different between MainActivity.this vs getApplicationContext()
Reference
http://stackoverflow.com/questions/13143006/alert-dialog-from-within-onoptionsitemselected-androidhttp://stackoverflow.com/questions/22966601/what-is-different-between-mainactivity-this-vs-getapplicationcontext
No comments :
Post a Comment