F.Peconi F.Peconi - 1 month ago 11
Android Question

Android app crashes after startActivity method is called from Onclick callback, manifest seems ok

I am writing my first Android app and i am stucked with this problem:

I'd like to switch from MainActivity to AddExpense activity after a FloatingButton is clicked so i wrote this code( inside

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 class looks easily like this

public class AddExpense extends AppCompatActivity {

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


}

However the application keeps crashing after i click on the fab.

Looking through other answers i found that usually the problem lies inside the manifest, but i think this is not my case:

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




Any help would be extremely appreciated.

--EDIT--
logcat report:

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) 

Answer Source

in your xml try adding a theme tag like this:

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