-3

When I press a button on the main menu of my app to launch a new activity, the whole app crashes and I am not too sure why. It is the efficiency activity. These two files are called the activity_efficiency.xml and the Efficiency.java class.

public void goEfficient(View v){
    if(v.getId()==R.id.efficiencyID){
        Intent goefficient = new Intent(MainActivity.this, Efficiency.class);
        startActivity(goefficient);
    }
}

the snippet above, it what is called from the main activity. This works fine, its the next section of the activity which causes it to crash I believe:

package com.laminar.calculator.calculator;

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

public class Efficiency extends AppCompatActivity {

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

}
public void workout(View a){
    EditText EO = (EditText)findViewById(R.id.energyoutput);
    EditText EI = (EditText)findViewById(R.id.energyinput);
    TextView VIEW = (TextView)findViewById(R.id.textView15);

    if(EO.length()==0||EI.length()==0){
        Snackbar.make(a, "Please enter your values into the equation", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }else{

        double oe = Double.parseDouble(EO.getText().toString());
        double ei = Double.parseDouble(EI.getText().toString());
        double stage1 = oe / ei;
        double stage2 = stage1 * 100;

        VIEW.setText("Efficiency: "+stage2+"J");
    }
}

}

And then, this is the activity.efficiency.xml text:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Efficiency">

<TextView
    android:id="@+id/textView13"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Work out the efficiency of an object"
    android:textAppearance="@android:style/TextAppearance.Material.Medium" 
/>

<EditText
    android:id="@+id/energyoutput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Enter useful energy output here (J)"
    android:inputType="number|numberSigned|numberDecimal" />

<EditText
    android:id="@+id/energyinput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Enter input energy here (J)"
    android:inputType="number|numberSigned|numberDecimal" />

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

<TextView
    android:id="@+id/textView15"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@android:style/TextAppearance.Material.Medium" 
/>

<ImageView
    android:id="@+id/imageView11"
    android:layout_width="match_parent"
    android:layout_height="87dp"
    app:srcCompat="@drawable/efficiency2" />

</LinearLayout>

Just to clarify, every other activity in my app loads up fine. The complete code for my app can be found on my GitHub here: My Remote Repository

Heres the Run Log on the crash:

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6809)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
               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.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:207)
                  at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                  at com.laminar.calculator.calculator.Efficiency.onCreate(Efficiency.java:18)
                  at android.app.Activity.performCreate(Activity.java:6998)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046) 
                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688) 
                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6809) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

If any extra information is needed to fix this problem, please do not hesitate to leave a comment.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Patalayus
  • 13
  • 1
  • 4

2 Answers2

0

It turns out I needed to get rid of the secondary Toolbar which I had created. I was trying to add a second toolbar to the activity.

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

}
Patalayus
  • 13
  • 1
  • 4
-3

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

Looks like you're trying to set an action bar for an activity that already has one. Do what the log says and set windowActionBar to false in your styles.xml (or whatever it's called) file, like so:

res/values/styles.xml (redacted):

<style name="MyAppTheme" parent="Theme.AppCompat">
    <!-- ... -->
    <item name="windowActionBar">false</item>
</style>

That should allow you to use your own.

Edric
  • 24,639
  • 13
  • 81
  • 91
jburn2712
  • 167
  • 1
  • 7