簡體   English   中英

我的Android應用意外關閉

[英]My Android App shutdown unexpectedly

從你們那里獲得一些幫助之后,我構建了一個很棒的App(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" />

感謝您的支持。

PS我是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

在調用方法之前,使用findViewById()初始化alterarAltura

您需要在將字段分配給變量之后調用.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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM