Monstruone
Monstruone

Reputation: 19

This Activity already has an action bar supplied by the window decor Error on Android Studio

I'm trying to launch a new Activity from my main one. Every time I execute it I get,

Unfortunately, My application has Stopped.

I´ve been going through similar questions but still can't solve my problem.

Here's the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usuario.myapplication">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
    <activity
        android:name=".secondActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>
</manifest>

Java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView texto;
    EditText editText;
    Button boton;
    Button boton2;

    private GoogleApiClient client;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case (R.id.boton):
                String dato = editText.getText().toString();
                Toast.makeText(getApplicationContext(), dato, Toast.LENGTH_SHORT).show();
                texto.setText(dato);
                break;
            case (R.id.boton2):
                Intent intent = new Intent(MainActivity.this, secondActivity.class);
                startActivity(intent);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

        texto = (TextView)findViewById(R.id.texto);
        editText = (EditText)findViewById(R.id.editText);
        boton = (Button)findViewById(R.id.boton);
        boton2 = (Button)findViewById(R.id.boton2);
        boton.setOnClickListener(this);
        boton2.setOnClickListener(this);



    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.usuario.myapplication/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.usuario.myapplication/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.usuario.myapplication.MainActivity">

    <TextView
        android:id="@+id/texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="ingresar dato" />

    <Button
        android:id="@+id/boton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/boton" />

    <Button
        android:id="@+id/boton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="new activity" />

</LinearLayout>

And here is the Logcat:

01-24 14:27:42.042 8033-8033/com.example.usuario.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.usuario.myapplication, PID: 8033
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usuario.myapplication/com.example.usuario.myapplication.secondActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      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: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                      at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                      at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
                      at com.example.usuario.myapplication.secondActivity.onCreate(secondActivity.java:17)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      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) 

Second Activity:

package com.example.usuario.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class secondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

Upvotes: 1

Views: 6014

Answers (6)

Idara Duff
Idara Duff

Reputation: 1

    The problem is in your Manifest file.

Change the following portion of your Manifest xml file from this; enter code here

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
    android:name=".secondActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>'

to this;

<activity android:name=".MainActivity">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
    android:name=".secondActivity"
<!--Add the following line to each of your activities in the manifest-->
  android:theme="@style/AppTheme.NoActionBar">
</activity>'

Upvotes: 0

Amit Vaghela
Amit Vaghela

Reputation: 22945

In your AppTheme style use,

Theme.AppCompat.Light.NoActionBar

instead of

Theme.AppCompat.Light.DarkActionBar

You are setting a theme that has an actionbar and then you are setting toolbar as an actionbar. That's why your are getting this error.

Use the theme that has no actionbar instead and it will fix the issue.

Upvotes: 0

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Since you already have a Toolbar Use this (In your Styles):

Theme.AppCompat.Light.NoActionBar

Example:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

And also, add these two:

<item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>

According to:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Perhaps you wanna try this also in your OnCreate:

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case (R.id.boton):
                String dato = editText.getText().toString();
                Toast.makeText(getApplicationContext(), dato, Toast.LENGTH_SHORT).show();
                texto.setText(dato);
                break;
            case (R.id.boton2):
                Intent intent = new Intent(MainActivity.this, secondActivity.class);
                startActivity(intent);
                break;
        }
    }

Upvotes: 0

Amit Kumar
Amit Kumar

Reputation: 1458

Remove this from your Manifest file:

<intent-filter>
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Upvotes: 0

user3736529
user3736529

Reputation:

As per logcat log; there is a confliction between implementation of Manifest file and SecondActivity class.

You got two solutions here but implement either of them :

  1. You can remove the complete two line "Toolbar" implementation code.

  2. You may make this change in manifest file as below :

             android:theme="@style/AppTheme.NoActionBar"
    

Hope it would help you out.

Cheers!

Upvotes: 3

gi097
gi097

Reputation: 7703

Change:

<activity
        android:name=".secondActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

To:

<activity
    android:name=".secondActivity"
    android:launchMode="singleTask" />

Upvotes: 0

Related Questions