2017-08-19 5 views
0

ich meine erste Android App schreibe, und ich bin mit diesem Problem fest:Android App stürzt nach startActivity Methode von Onclick Rückruf aufgerufen wird, manifestieren scheint ok

Ich möchte von MainActivity-AddExpense wechseln Aktivität nach einer FloatingButton geklickt wird, so schrieb ich diesen Code (innen protected void onCreate(Bundle savedInstanceState):

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getApplicationContext(), AddExpense.class); 
      startActivity(intent); 
     } 
    }); 

AddExpense cla s sieht leicht wie diese

public class AddExpense extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add_expense); 
} 

}

jedoch die Anwendung andauernd abstürzt, nachdem ich auf der fab klicken.
durch andere Antworten der Suche fand ich, dass in der Regel das Problem innerhalb des manifesten liegt, aber ich denke, das ist nicht mein Fall ist:

<?xml version="1.0" encoding="utf-8"?> 

<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" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".AddExpense" 
     android:label="@string/app_name"></activity> 
</application> 

Jede Hilfe geschätzt extrem würde .

--EDIT-- logcat Bericht:

08-19 18:10:02.802 10393-10393/com.example.senso.budgetracker E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.senso.budgetracker, PID: 10393 
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.senso.budgetracker/com.example.senso.budgetracker.AddExpense}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2728) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814) 
                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:154) 
                       at android.app.ActivityThread.main(ActivityThread.java:6290) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                      Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
                       at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:356) 
                       at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:325) 
                       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:286) 
                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                       at com.example.senso.budgetracker.AddExpense.onCreate(AddExpense.java:11) 
                       at android.app.Activity.performCreate(Activity.java:6760) 
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)  
                       at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:154)  
                       at android.app.ActivityThread.main(ActivityThread.java:6290)  
                       at java.lang.reflect.Method.invoke(Native Method)  
+1

show logcat Fehlermeldung – umuieme

+0

Sehen Sie sich die [stackoverflow] (http://stackoverflow.com/questions/23353173) an, um die Ursache des Absturzes festzustellen. –

+0

ist activity_add_expense gültig? – cliff2310

Antwort

0

in Ihrem xml versucht, ein Thema Tag wie folgt ergänzt:

<activity android:name=".AddExpense" 
    android:label="@string/app_name" 
    android:parentActivityName=".MainActivity" 
    android:theme="@style/AppTheme.NoActionBar"> 
    <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="blah.blah.com.Myproject.MainActivity" // your package replace 
     /> 

</activity> 
+0

Ich weiß nicht warum, aber es hat funktioniert! Danke, mein Herr. –

+0

genial kannst du es als beantwortet markieren :) viel Glück. Der Grund, warum es nicht funktionierte, ist, dass Sie die Elternaktivität nicht verknüpft haben. –

+1

@ F.Peconi, können Sie das "" -Tag entfernen. Sie brauchen nur das 'android: theme' –

0

ich herausgefunden, es ist eine Syntax Problem ist!
Irgendwie Bearbeitung des Manifests, änderte ich

<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"> 

in

<application> 
android:allowBackup="true" 
... 
android:theme="@style/AppTheme"> 

sofort die Anwendung Tag zu schließen und die Attribute keine parsable verlassen.
Es war genug, um das Tag richtig zu schließen, damit alles funktioniert.

Verwandte Themen