基本应用程序 - IllegalStateException:已经附加

时间:2016-11-02 04:04:57

标签: java android android-layout

代码似乎没问题。但是,当我在模拟器中运行它时,应用程序无法正常工作。

代码:

package com.example.claudia.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


@Override// Calculating Body mass index
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button_Calc = (Button) findViewById(R.id.button_Calc);
    final EditText Field_Weight = (EditText)      findViewById(R.id.Field_Weight);
    final EditText Field_Height = (EditText) findViewById(R.id.Field_Height);
    final TextView View_Result = (TextView) findViewById(R.id.View_Results);
    final TextView View_Msg = (TextView) findViewById(R.id.View_Msg);

    button_Calc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double weight;
            double height;
            double bmi;
            String msg = "";

            if (Field_Height.getText().toString().equals("") || Field_Weight.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(), "No Valid Values!", Toast.LENGTH_LONG).show();


                weight = Double.parseDouble(Field_Weight.getText().toString());
                height = Double.parseDouble(Field_Height.getText().toString());

                bmi = height * height;
                bmi = weight / bmi;

                View_Result.setText(String.valueOf(bmi));

                if (bmi < 18.5) {
                    msg = "Client is Under Weight :(";
                } else if (bmi > 18.5 && bmi < 25) {
                    msg = "Normal :)";
                } else if (bmi > 25) {
                    msg = "Client is Overweight";
                }
                View_Msg.setText(msg);

            }


        }
    });


    // Calculating Calories in and out
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button Cal_calc = (Button) findViewById(R.id.Cal_calc);
        final EditText field_cal = (EditText) findViewById(R.id.field_Cal);
        final EditText field_burn = (EditText) findViewById(R.id.field_burn);
        final TextView View_calRes = (TextView) findViewById(R.id.View_calRes);
        final TextView View_calMsg = (TextView) findViewById(R.id.view_calMsg);

        Cal_calc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double calories;
                double burn;
                double CaloricTotal;
                String calMsg = "";

                if (field_cal.getText().toString().equals("") || field_burn.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "No Valid Values!", Toast.LENGTH_LONG).show();


                    calories = Double.parseDouble(field_cal.getText().toString());
                    burn = Double.parseDouble(field_burn.getText().toString());

                    CaloricTotal = calories * calories;
                    CaloricTotal = burn / CaloricTotal;

                    View_calRes.setText(String.valueOf(CaloricTotal));

                    if (CaloricTotal < 1500) {
                        calMsg = "Client Needs to consume more Calories :(";
                    } else if (CaloricTotal > 1500 && CaloricTotal < 2000) {
                        calMsg = "Client is Normal :)";
                    } else if (CaloricTotal > 2000) {
                        calMsg = "Client Needs to consume less calories";
                    }
                    View_calMsg.setText(calMsg);

                }



            }
        });
    }
    }}

RUN错误::

11/01 22:57:40: Launching app
$ adb shell am start -n "com.example.claudia.myapplication/com.example.claudia.myapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 3591 on device emulator-5554
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Unexpected CPU variant for X86 using defaults: x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.claudia.myapplication-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.claudia.myapplication, real application class is null.

              [ 11-01 22:53:09.592  1515: 1538 D/         ]
              HostConnection::get() New Host Connection established 0x95618500, tid 1538
W/System: ClassLoader referenced unknown path: /data/app/com.example.claudia.myapplication-2/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.claudia.myapplication, PID: 3591
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.claudia.myapplication/com.example.claudia.myapplication.MainActivity}: java.lang.IllegalStateException: Already attached
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.IllegalStateException: Already attached
                      at android.support.v4.app.FragmentManagerImpl.attachController(FragmentManager.java:2132)
                      at android.support.v4.app.FragmentController.attachHost(FragmentController.java:104)
                      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315)
                      at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
                      at com.example.claudia.myapplication.MainActivity.onCreate(MainActivity.java:63)
                      at android.app.Activity.performCreate(Activity.java:6664)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

申请已终止。

2 个答案:

答案 0 :(得分:0)

在onCreate方法中,

super.onCreate()setContentView被调用两次。此外,还有不必要的括号。

答案 1 :(得分:0)

正如其他答案所述,由于多个原因导致Exception被抛出:

  • super.onCreate()被叫两次
  • setContentView()被叫两次
  • 多种语法错误。

FIX

试试这段代码:

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


@Override// Calculating Body mass index
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button_Calc = (Button) findViewById(R.id.button_Calc);
    final EditText Field_Weight = (EditText)findViewById(R.id.Field_Weight);
    final EditText Field_Height = (EditText)findViewById(R.id.Field_Height);
    final TextView View_Result = (TextView)findViewById(R.id.View_Results);
    final TextView View_Msg = (TextView)findViewById(R.id.View_Msg);
    final Button Cal_calc = (Button)findViewById(R.id.Cal_calc);
    final EditText field_cal = (EditText)findViewById(R.id.field_Cal);
    final EditText field_burn = (EditText)findViewById(R.id.field_burn);
    final TextView View_calRes = (TextView)findViewById(R.id.View_calRes);
    final TextView View_calMsg = (TextView)findViewById(R.id.view_calMsg);

    button_Calc.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            double weight;
            double height;
            double bmi;
            String msg = "";

            if (Field_Height.getText().toString().equals("") || Field_Weight.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(), "No Valid Values!", Toast.LENGTH_LONG).show();


                weight = Double.parseDouble(Field_Weight.getText().toString());
                height = Double.parseDouble(Field_Height.getText().toString());

                bmi = height * height;
                bmi = weight / bmi;

                View_Result.setText(String.valueOf(bmi));

                if (bmi < 18.5) {
                    msg = "Client is Under Weight :(";
                } else if (bmi > 18.5 && bmi < 25) {
                    msg = "Normal :)";
                } else if (bmi > 25) {
                    msg = "Client is Overweight";
                }
                View_Msg.setText(msg);

            }


        }
    });


    Cal_calc.setOnClickListener(new View.OnClickListener() 
    {
            @Override
            public void onClick(View v) {
                double calories;
                double burn;
                double CaloricTotal;
                String calMsg = "";

                if (field_cal.getText().toString().equals("") || field_burn.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "No Valid Values!", Toast.LENGTH_LONG).show();


                    calories = Double.parseDouble(field_cal.getText().toString());
                    burn = Double.parseDouble(field_burn.getText().toString());

                    CaloricTotal = calories * calories;
                    CaloricTotal = burn / CaloricTotal;

                    View_calRes.setText(String.valueOf(CaloricTotal));

                    if (CaloricTotal < 1500) {
                        calMsg = "Client Needs to consume more Calories :(";
                    } else if (CaloricTotal > 1500 && CaloricTotal < 2000) {
                        calMsg = "Client is Normal :)";
                    } else if (CaloricTotal > 2000) {
                        calMsg = "Client Needs to consume less calories";
                    }
                    View_calMsg.setText(calMsg);

                }



            }
    });

}

}

注意:请检查是否缺少任何大括号,如果缺少任何大括号,请添加。