2016-05-01 22 views
0

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; 
    } 
} 
+0

Haben Sie versucht, das Thema zu AppCompat zu ändern? –

+1

verwenden Sie 'getApplicationContext()'? –

+0

Teilen Sie Ihren Dialogerstellungscode. –

Antwort

1

Sie

nicht
MyApp.getContext(); 

Erstellen Sie einen Konstruktor verwenden und einen Kontext wie

public LegalProsecution(Context context){ 
    this.context = context; 
} 

dann zuweisen, wenn Sie eine Instanz erstellen, wie diese

LegalProsecution lp = new LegalProsecution(MyActivity.this); 

Dann don erstellen Vergessen Sie nicht, den Alarmdialog mit der context zu erstellen, die Sie gerade

01 zugewiesen haben
AlertDialog.Builder builder1 = new AlertDialog.Builder(context); 
+0

Okay, ich werde das versuchen. Danke für den Tipp. – ideaman924

+0

Okay, bei public legalprosecution (Kontextkontext) erhalte ich den Fehler "Klasse oder Schnittstelle erforderlich". Irgendeine Hilfe? – ideaman924

+0

@ ideaman924 Nein, es sollte diese Art von Fehler nicht zeigen.Sind Sie sicher, dass Sie diese Methode am Anfang der LegalPrsecution-Klasse erstellt haben? –

0

Ich denke, die Aktivität, auf die Sie das Dialogdesign anwenden möchten, ist die Erweiterung ActionBarActivity. Versuchen Sie, stattdessen AppCompat zu erweitern.

+0

'ActionBarActivity' war veraltet. –

+0

@ShreeKrishna ja ich weiß, aber einige Entwickler verwenden es immer noch –

+0

Dies wird das Problem nicht lösen .. Es würde es noch schlimmer machen. ActionBarActivity ist ein Boner. – ideaman924

0

Vergewissern Sie sich, dass das App-Design auf AppCompat android:theme="@style/Theme.AppCompat.light eingestellt ist. Wenn das stimmt, liegt das wahrscheinlich an der Vererbung Ihrer Hauptklasse, die das Thema auf ein anderes Thema gesetzt hat.

+0

Theme ist auf Light Dark Actionbar eingestellt, was Teil von appcompat ist, also keine Probleme. – ideaman924

Verwandte Themen