单击按钮后,我的应用程序崩溃了

时间:2018-04-02 16:56:14

标签: android android-activity

当我按下我的应用程序主菜单上的按钮以启动新活动时,整个应用程序崩溃,我不太清楚为什么。这是效率活动。这两个文件称为activity_efficiency.xmlEfficiency.java类。

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

上面的代码片段,它是从主要活动中调用的内容。这工作正常,它的活动的下一部分导致它崩溃我相信:

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");
    }
}

}

然后,这是activity.efficiency.xml文字:

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

为了澄清,我的应用中的其他所有活动都很好。我的应用程序的完整代码可以在我的GitHub上找到:My Remote Repository

继续运行登录崩溃:

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) 

如果需要任何额外信息来解决此问题,请随时发表评论。

2 个答案:

答案 0 :(得分:0)

事实证明我需要摆脱我创建的辅助工具栏。 我试图在活动中添加第二个工具栏。

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

}

答案 1 :(得分:-3)

  

java.lang.IllegalStateException:此Activity已经有一个动作   酒吧提供的窗户装饰。不要求   Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false   你的主题改为使用工具栏

您似乎正在尝试为已有活动的活动设置操作栏。执行日志说明并在$ yarn config set yarn-offline-mirror /tmp/npm-packages-offline-cache $ yarn install $ ls /tmp/npm-packages-offline-cache (或其所谓的)文件中将$ yarn config set yarn-offline-mirror vendor/npm-packages-offline-cache $ cd ~/my_project $ yarn install $ ls ~/my_project/vendor/npm-packages-offline-cache 设置为windowActionBar,如下所示:

false(编辑):

styles.xml

这应该允许你使用自己的。