我刚才遇到了这个问题,抓住了如何解决这个问题。
基本上,我有MainActivity ,以及我的应用程序中的很多类,如RootUtils , LegalProsecution , CameraSilencer等等CameraSilencer是这个应用程序中唯一的活动,其余只是用于执行代码,显示对话框等的类。
我LegalProsecution.java问题的特定类是LegalProsecution.java ,它负责在首次启动时打开一个对话框并向用户显示合法的内容。 但是,堆栈跟踪指向第48行,这只是dialog.show();
这是完整的堆栈跟踪:
05-01 19:01:08.075 21777-21777/ideaman924.camerasilencer E/AndroidRuntime: FATAL EXCEPTION: main Process: ideaman924.camerasilencer, PID: 21777 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{ideaman924.camerasilencer/ideaman924.camerasilencer.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5458) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: 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:340) at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273) at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80) at android.support.v7.app.AlertController.installContent(AlertController.java:214) at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:256) at android.app.Dialog.dispatchOnCreate(Dialog.java:394) at android.app.Dialog.show(Dialog.java:295) at ideaman924.camerasilencer.LegalProsecution.warningShow(LegalProsecution.java:48) at ideaman924.camerasilencer.MainActivity.onCreate(MainActivity.java:20) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5458) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)有谁知道这是什么问题? 谢谢。
编辑:这是LegalProsecution.java,它处理对话框创建:
package ideaman924.camerasilencer; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; public class LegalProsecution { Context context = MyApp.getContext(); AppPreference appprefs = AppPreference.getInstance(context); public void warningShow() { if(appprefs.loadSettings() == 1); else { AlertDialog.Builder builder1 = new AlertDialog.Builder(MyApp.getContext()); builder1.setTitle(context.getResources().getString(R.string.warning)); builder1.setMessage(context.getResources().getString(R.string.warning_description)); builder1.setCancelable(true); builder1.setPositiveButton( context.getResources().getString(R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Got promise from user, now setting first_run to 1 appprefs.storeSettings(1); dialog.cancel(); } } ); builder1.setNegativeButton( context.getResources().getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Okay, cool, bye! No CS for you! dialog.cancel(); ((Activity)context).finish(); System.exit(0); } } ); AlertDialog alert1 = builder1.create(); alert1.show(); } } }这里是MyApp.java,我用它作为解决愚蠢的上下文问题的解决方法:
package ideaman924.camerasilencer; import android.app.Application; import android.content.Context; public class MyApp extends Application { private static MyApp instance; public static MyApp getInstance() { return instance; } public static Context getContext(){ return instance.getApplicationContext(); } @Override public void onCreate() { super.onCreate(); instance = this; } }I ran into this problem just now, scratching my head on how to tackle this problem.
Basically, I have MainActivity, and a lot of classes in my app, such as RootUtils, LegalProsecution, CameraSilencer, etc, etc. MainActivity is the only activity in this app, and the rest is just classes for executing code, showing dialogs, etc.
The particular class I have problem with is LegalProsecution.java, which is responsible for opening a dialog on first launch and show the users legal stuff. However, the stack trace points to line 48, which is simply dialog.show();
Here is the full stack trace:
05-01 19:01:08.075 21777-21777/ideaman924.camerasilencer E/AndroidRuntime: FATAL EXCEPTION: main Process: ideaman924.camerasilencer, PID: 21777 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{ideaman924.camerasilencer/ideaman924.camerasilencer.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5458) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: 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:340) at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273) at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80) at android.support.v7.app.AlertController.installContent(AlertController.java:214) at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:256) at android.app.Dialog.dispatchOnCreate(Dialog.java:394) at android.app.Dialog.show(Dialog.java:295) at ideaman924.camerasilencer.LegalProsecution.warningShow(LegalProsecution.java:48) at ideaman924.camerasilencer.MainActivity.onCreate(MainActivity.java:20) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5458) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)Anybody know what's the problem? Thanks.
EDIT: Here is LegalProsecution.java, which handles dialog creation:
package ideaman924.camerasilencer; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; public class LegalProsecution { Context context = MyApp.getContext(); AppPreference appprefs = AppPreference.getInstance(context); public void warningShow() { if(appprefs.loadSettings() == 1); else { AlertDialog.Builder builder1 = new AlertDialog.Builder(MyApp.getContext()); builder1.setTitle(context.getResources().getString(R.string.warning)); builder1.setMessage(context.getResources().getString(R.string.warning_description)); builder1.setCancelable(true); builder1.setPositiveButton( context.getResources().getString(R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Got promise from user, now setting first_run to 1 appprefs.storeSettings(1); dialog.cancel(); } } ); builder1.setNegativeButton( context.getResources().getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Okay, cool, bye! No CS for you! dialog.cancel(); ((Activity)context).finish(); System.exit(0); } } ); AlertDialog alert1 = builder1.create(); alert1.show(); } } }And here is MyApp.java, which I used as a workaround to solve the stupid context issue:
package ideaman924.camerasilencer; import android.app.Application; import android.content.Context; public class MyApp extends Application { private static MyApp instance; public static MyApp getInstance() { return instance; } public static Context getContext(){ return instance.getApplicationContext(); } @Override public void onCreate() { super.onCreate(); instance = this; } }最满意答案
不要用
MyApp.getContext();创建一个构造函数,并指定一个上下文
public LegalProsecution(Context context){ this.context = context; }然后,当您创建实例时,像这样创建
LegalProsecution lp = new LegalProsecution(MyActivity.this);然后不要忘记使用刚刚分配给的context创建警报对话框
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);Don't use
MyApp.getContext();Create a constructor, and assign a context like
public LegalProsecution(Context context){ this.context = context; }Then when you create an instance, Create like this
LegalProsecution lp = new LegalProsecution(MyActivity.this);Then don't forget to create alert dialog using the context you just assigned to
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);更多推荐
发布评论