我的Android应用程序意外关闭

时间:2014-03-04 21:37:07

标签: java android listener

在得到你们的帮助之后,我建立了一个很棒的应用程序(BMI计算器)。添加搜索栏并清除代码中的所有错误后,应用程序在我测试时无法启动,显示消息"应用程序无法正常关闭"。

搜索栏是一种可选方法,用于更改textedit字段中的值。

这是MainActiviy.java

package com.example.calculadorimc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
    private RadioGroup rgsexo;
    EditText editPeso;
    EditText editAltura;
    TextView imcView;
    SeekBar alterarAltura;
    SeekBar alterarPeso;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    alterarAltura.setOnSeekBarChangeListener(alteraralturaListener);
    editPeso = (EditText)findViewById(R.id.editPeso);
    editAltura = (EditText)findViewById(R.id.editAltura);
    imcView = (TextView)findViewById(R.id.imcView);
    alterarAltura = (SeekBar)findViewById(R.id.alterarAltura);
    alterarPeso = (SeekBar)findViewById(R.id.alterarPeso);
}

private OnSeekBarChangeListener alteraralturaListener = new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
        // TODO Auto-generated method stub
        // Calcula o novo valor do TIP

        double setAltura = (alterarAltura.getProgress()) * .01d;
        // mostra na caixa o valor novo
        editAltura.setText(String.format("%.02f", setAltura).replace(',', '.'));
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }
};
public void calculateClickHandler(View view) {

    // make sure we handle the click of the calculator button

    if (view.getId() == R.id.botaoCalcular) {

     // get the users values from the widget references

     float peso = Float.parseFloat(editPeso.getText().toString());
     float altura = Float.parseFloat(editAltura.getText().toString());

     // calculate the bmi value

     float imcValue = calcularIMC(peso, altura);

     // interpret the meaning of the bmi value
     String imcInterpretation = interpretIMC(imcValue);

     // now set the value in the result text

     imcView.setText(imcValue + "-" + imcInterpretation);
    }
   }
   // the formula to calculate the BMI index

   // check for http://en.wikipedia.org/wiki/Body_mass_index
   private float calcularIMC (float peso, float altura) {

    return (float) (peso / (altura * altura));
   }

   // interpret what BMI means
   private String interpretIMC(float imcValue) {           

       rgsexo = (RadioGroup)findViewById(R.id.rgSexo);
       int selectedId = rgsexo.getCheckedRadioButtonId();  // get the id

       switch (selectedId)   // switch on the button selected
       {
            case R.id.radioMasc:
                if (imcValue < 20) {
                    return "Abaixo do Peso";
                   } else if (imcValue < 24.9) {

                    return "Peso Normal";
                   } else if (imcValue < 29.9) {

                    return "Acima do Peso";
                   } else if (imcValue < 39.9) {

                    return "Obesidade Moderada";
                   } else {
                    return "Obesidade Mórbida";
                   }
            case R.id.radioFem:
                if (imcValue < 19) {
                    return "Abaixo do Peso";
                   } else if (imcValue < 23.9) {

                    return "Peso Normal";
                   } else if (imcValue < 28.9) {

                    return "Acima do Peso";
                   } else if (imcValue < 38.9) {

                    return "Obesidade Moderada";
                   } else {
                    return "Obesidade Mórbida";
                   }
       }
    return null;
   }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

和Activitymain.xml

<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/rgSexo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radioMasc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/radioMasc" />

    <RadioButton
        android:id="@+id/radioFem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioFem" />
</RadioGroup>

<TextView
    android:id="@+id/alturaView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/alturaView" />

<EditText
    android:id="@+id/editAltura"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="5"
    android:inputType="numberDecimal"
    android:text="@string/editAltura" >

    <requestFocus />
</EditText>

<SeekBar
    android:id="@+id/alterarAltura"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="3" />

<TextView
    android:id="@+id/pesoView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pesoView" />

<EditText
    android:id="@+id/editPeso"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="5"
    android:inputType="number"
    android:text="@string/editPeso" />

<SeekBar
    android:id="@+id/alterarPeso"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="600" />

<View
    android:layout_width="fill_parent"
    android:layout_height="2dip"
    android:layout_marginBottom="5dip"
    android:layout_marginTop="5dip"
    android:background="#111111" />

<Button
    android:id="@+id/botaoCalcular"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="calculateClickHandler"
    android:text="@string/botaoCalcular" />

<TextView
    android:id="@+id/imcView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/imcView"
    android:textAppearance="?android:attr/textAppearanceLarge" />

感谢您的支持。

P.S。我总计N00b

LogCat文件

    03-04 21:18:38.492: D/AndroidRuntime(279): Shutting down 

VM
03-04 21:18:38.492: W/dalvikvm(279): threadid=1: thread 

exiting with uncaught exception (group=0x4001d800)
03-04 21:18:38.542: E/AndroidRuntime(279): FATAL 

EXCEPTION: main
03-04 21:18:38.542: E/AndroidRuntime(279): 

java.lang.RuntimeException: Unable to start activity 

ComponentInfo

{com.example.calculadorimc/com.example.calculadorimc.Main

Activity}: java.lang.NullPointerException
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:2663)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.ActivityThread.handleLaunchActivity

(ActivityThread.java:2679)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.ActivityThread.access$2300

(ActivityThread.java:125)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.ActivityThread$H.handleMessage

(ActivityThread.java:2033)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.os.Handler.dispatchMessage(Handler.java:99)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.os.Looper.loop(Looper.java:123)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.ActivityThread.main(ActivityThread.java:4627)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

java.lang.reflect.Method.invokeNative(Native Method)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

java.lang.reflect.Method.invoke(Method.java:521)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

com.android.internal.os.ZygoteInit

$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

com.android.internal.os.ZygoteInit.main

(ZygoteInit.java:626)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

dalvik.system.NativeStart.main(Native Method)
03-04 21:18:38.542: E/AndroidRuntime(279): Caused by: 

java.lang.NullPointerException
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

com.example.calculadorimc.MainActivity.onCreate

(MainActivity.java:27)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.Instrumentation.callActivityOnCreate

(Instrumentation.java:1047)
03-04 21:18:38.542: E/AndroidRuntime(279):  at 

android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:2627)
03-04 21:18:38.542: E/AndroidRuntime(279):  ... 11 

more
03-04 21:18:44.231: I/Process(279): Sending signal. PID: 

279 SIG: 9
03-04 21:18:48.231: D/AndroidRuntime(286): Shutting down 

VM
03-04 21:18:48.231: W/dalvikvm(286): threadid=1: thread 

exiting with uncaught exception (group=0x4001d800)
03-04 21:18:48.272: E/AndroidRuntime(286): FATAL 

EXCEPTION: main
03-04 21:18:48.272: E/AndroidRuntime(286): 

java.lang.RuntimeException: Unable to start activity 

ComponentInfo

{com.example.calculadorimc/com.example.calculadorimc.Main

Activity}: java.lang.NullPointerException
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:2663)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.ActivityThread.handleLaunchActivity

(ActivityThread.java:2679)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.ActivityThread.access$2300

(ActivityThread.java:125)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.ActivityThread$H.handleMessage

(ActivityThread.java:2033)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.os.Handler.dispatchMessage(Handler.java:99)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.os.Looper.loop(Looper.java:123)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.ActivityThread.main(ActivityThread.java:4627)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

java.lang.reflect.Method.invokeNative(Native Method)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

java.lang.reflect.Method.invoke(Method.java:521)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

com.android.internal.os.ZygoteInit

$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

com.android.internal.os.ZygoteInit.main

(ZygoteInit.java:626)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

dalvik.system.NativeStart.main(Native Method)
03-04 21:18:48.272: E/AndroidRuntime(286): Caused by: 

java.lang.NullPointerException
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

com.example.calculadorimc.MainActivity.onCreate

(MainActivity.java:27)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.Instrumentation.callActivityOnCreate

(Instrumentation.java:1047)
03-04 21:18:48.272: E/AndroidRuntime(286):  at 

android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:2627)
03-04 21:18:48.272: E/AndroidRuntime(286):  ... 11 

more
03-04 21:18:52.073: I/Process(286): Sending signal. PID: 

286 SIG: 9

2 个答案:

答案 0 :(得分:2)

在调用方法之前用alterarAltura初始化findViewById()

答案 1 :(得分:2)

您需要在将字段分配给变量后调用.setOnSeek...,否则alterarAltura将为null并且null上的操作会导致NullPointerException

示例:

    editPeso = (EditText)findViewById(R.id.editPeso);
    editAltura = (EditText)findViewById(R.id.editAltura);
    imcView = (TextView)findViewById(R.id.imcView);
    alterarAltura = (SeekBar)findViewById(R.id.alterarAltura);
    alterarPeso = (SeekBar)findViewById(R.id.alterarPeso);
    alterarAltura.setOnSeekBarChangeListener(alteraralturaListener);