0

I am writing my first Android app and I am stuck 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) 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
F.Peconi
  • 56
  • 9

2 Answers2

0

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>
Mohamoud Mohamed
  • 515
  • 7
  • 16
0

I figured out, it was a syntax problem !
Somehow, editing the manifest, i changed

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

closing immediately the application tag and leaving the attributes no parsable.
It was enough to close the tag properly to make everything work.

F.Peconi
  • 56
  • 9