无法启动活动组件信息 - 应用程序崩溃

时间:2016-09-05 01:50:21

标签: java android

我正在制作BMI计算器。在主要活动中,我有单选按钮来检查用户是否需要公制或英制。最近,我加入了一个计时器来确定用户何时完成打字以自动计算他们的BMI。英制计算工作得很好,但是当我点击公制单选按钮时,它会立即崩溃。错误消息“无法启动活动组件信息”。这种情况以前没有发生过。除了不同的测量值之外,代码应该非常相似。

感谢您的帮助!

以下是指标活动的代码:

public void performCalculation()
{
    EditText weightET = (EditText) findViewById(R.id.weightET);
    weightET.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        private Timer timer = new Timer();
        private final long DELAY = 500;

        @Override
        public void afterTextChanged(final Editable editable)
        {
            timer.cancel();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run()
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run()
                        {
                            calculate();
                        }
                    });
                }

            }, DELAY);
        }
    });
}

public void calculate()
{

    EditText heightET = (EditText) findViewById(R.id.height);
    EditText weightET = (EditText) findViewById(R.id.weight);
    TextView yourBMI = (TextView) findViewById(R.id.yourBMI);
    TextView bmiClass = (TextView) findViewById(R.id.bmiClass);

    double height;
    double weight;
    double bmi;
    double bmiRoundOff;

    weight = Double.parseDouble(weightET.getText().toString());
    height = Double.parseDouble(heightET.getText().toString());

    bmi = (weight) / (height * height);

    bmiRoundOff = Math.round(bmi * 100.00) / 100.00;

    yourBMI.setText("Your BMI Is " + String.valueOf(bmiRoundOff));

    if(bmiRoundOff < 18.5)
    {
        bmiClass.setText("Your BMI Class: Underweight");
    }
    if(bmiRoundOff > 18.5 && bmiRoundOff <= 24.9)
    {
        bmiClass.setText("Your BMI Class: Normal");
    }
    if(bmiRoundOff >= 25.0 && bmiRoundOff <= 29.0)
    {
        bmiClass.setText("Your BMI Class: Overweight!");
    }
    if(bmiRoundOff > 29.0)
    {
        bmiClass.setText("Your BMI Class: Obese");
    }

}

以下是帝国活动的代码:

public void performCalculation()
{
    EditText weightET = (EditText) findViewById(R.id.weightET);
    weightET.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        private Timer timer = new Timer();
        private final long DELAY = 500;

        @Override
        public void afterTextChanged(final Editable editable)
        {
            timer.cancel();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run()
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run()
                        {
                            calculate();
                        }
                    });
                }

            }, DELAY);
        }
    });
}

public void calculate()
{
    EditText weightET = (EditText) findViewById(R.id.weightET);
    EditText feetET = (EditText) findViewById(R.id.feetET);
    EditText inchesET = (EditText) findViewById(R.id.inchesET);
    TextView yourBMI = (TextView) findViewById(R.id.yourBMI);
    TextView bmiClass = (TextView) findViewById(R.id.bmiClass);

    double feet;
    double feetToInches;
    double inches;
    double totalInches;
    double weight;
    double bmi;
    double bmiRoundOff;

    weight = Double.parseDouble(weightET.getText().toString());
    feet = Double.parseDouble(feetET.getText().toString());
    inches = Double.parseDouble(inchesET.getText().toString());

    feetToInches = feet * 12;
    totalInches = feetToInches + inches;
    bmi = (weight * 703) / (totalInches * totalInches);

    bmiRoundOff = Math.round(bmi * 100.00) / 100.00;

    yourBMI.setText("Your BMI Is " + String.valueOf(bmiRoundOff));

    if(bmiRoundOff < 18.5)
    {
        bmiClass.setText("Your BMI Class: Underweight");
    }
    if(bmiRoundOff > 18.5 && bmiRoundOff <= 24.9)
    {
        bmiClass.setText("Your BMI Class: Normal");
    }
    if(bmiRoundOff >= 25.0 && bmiRoundOff <= 29.0)
    {
        bmiClass.setText("Your BMI Class: Overweight!");
    }
    if(bmiRoundOff > 29.0) {
        bmiClass.setText("Your BMI Class: Obese");
    }
}

public void main (View view)
{
    Intent intent = new Intent(getApplicationContext() , MainActivity.class);
    startActivity(intent);
}

以下是主要活动:

public void metric (View view)
{
    Intent i = new Intent(getApplicationContext() , metric_activity.class);
    startActivity(i);
}
public void imperial (View view)
{
    Intent i = new Intent(getApplicationContext() , imperial_activity.class);
    startActivity(i);
}

Logcat:

   Process: com.jdaapps.bmicalculator, PID: 2004
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jdaapps.bmicalculator/com.jdaapps.bmicalculator.metric_activity}: java.lang.NullPointerException
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:136)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                      Caused by: java.lang.NullPointerException
                                                                         at com.jdaapps.bmicalculator.metric_activity.performCalculation(metric_activity.java:23)
                                                                         at com.jdaapps.bmicalculator.metric_activity.onCreate(metric_activity.java:110)
                                                                         at android.app.Activity.performCreate(Activity.java:5231)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:136) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                         at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

我认为如果你在mainActivity中可以做到这一点,那么你的活动没有得到:MainActivity.this.startActivity(intent) 如果你不知道哪个是主要在listview中发生的活动,你可以定义一个类扩展应用程序,并在清单中将其定义为应用程序名称,并在类(Activity currentActivity)和每个活动中有一个变量 在oncreat和onresume方法中执行此操作(例如,您的静态类是G):G.currentActivity = this; 然后你可以像你一样在你想要的地方打电话:

G.currentActivity.startActivity(intent) ;