Ich stieß gerade jetzt auf dieses Problem und kratzte meinen Kopf auf, wie man dieses Problem anpackt.Hinzufügen einer Klasse zum Manifest in Android
Grundsätzlich habe ich MainActivity
, und viele Klassen in meiner Anwendung, wie RootUtils
, LegalProsecution
, CameraSilencer
, etc, etc. MainActivity
ist die einzige Aktivität in dieser App, und der Rest ist nur Klassen für die Ausführung von Code, zeigt Dialoge, etc.
Die besondere Klasse, mit der ich ein Problem habe, ist LegalProsecution.java
, die für das Öffnen eines Dialogfelds beim ersten Start verantwortlich ist und den Benutzern legales Zeug zeigt. die Stack-Trace Punkte auf 48 Linie jedoch, die dialog.show();
Hier einfach ist, ist der volle 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)
Jeder weiß, was das Problem ist? Vielen Dank.
EDIT: Hier ist LegalProsecution.java, die Dialogerstellung Griffe:
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();
}
}
}
Und hier ist MyApp.java, die ich als Behelfslösung verwendet, um den dummen Kontext Problem zu lösen:
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;
}
}
Haben Sie versucht, das Thema zu AppCompat zu ändern? –
verwenden Sie 'getApplicationContext()'? –
Teilen Sie Ihren Dialogerstellungscode. –