java.lang.runtimeException 无法启动 Activity componentinfo java.lang.nullpointerException android

标签 java android eclipse nullpointerexception runtimeexception

我正在制作一个简单的卡路里计算器,它给了我这个错误,我已经查看了代码并在此处搜索了解决方案,但我无法看出为什么没有运行。 我认为这是因为我没有初始化变量,所以我这样做了,但仍然遇到相同的错误,也许是旋转器的问题,我是使用旋转器的新手

这是代码:

public class CaloriesCalculator extends ActionBarActivity {

EditText etAge, etWeight, etHeight;
Button btnCalculate;
TextView tvResult;
Spinner spinnerGender, spinnerActivity;
String itemGender, itemActivity;
int Height=0;
int Weight=0;
int Age=0;;
double bmr=0.0;
double tdee=0.0;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.caloriescalculator);

    spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
    spinnerActivity=(Spinner)findViewById(R.id.spinnerActivity);

    etAge=(EditText)findViewById(R.id.etAge);
    etWeight=(EditText)findViewById(R.id.etWeight);
    etHeight=(EditText)findViewById(R.id.etHeight);
    tvResult=(TextView)findViewById(R.id.tvResult);

    List<String> list = new ArrayList<String>();
    list.add("Male");
    list.add("Female");


    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, list );
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerGender.setAdapter(dataAdapter);


        List<String> list2 = new ArrayList<String>();
        list.add("Sedentary");
        list.add("Lightly Active");
        list.add("Moderalety Active");
        list.add("Very Active");
        list.add("Extremely Active");

        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, list2 );
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerActivity.setAdapter(dataAdapter2);

            btnCalculate.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    spinnerGender.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {


                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub

                        }

                    });

                    spinnerActivity.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub

                        }
                    });


                    itemGender=String.valueOf(spinnerGender.getSelectedItem());
                    itemActivity=String.valueOf(spinnerActivity.getSelectedItem());

                    if(itemGender=="Male"){

                        Weight=Integer.parseInt(etWeight.getText().toString());
                        Height=Integer.parseInt(etHeight.getText().toString());
                        Age=Integer.parseInt(etAge.getText().toString());

                        if(itemActivity=="Sedentary"){

                            bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
                            tdee=bmr*1.2;



                        }

                        else if(itemActivity=="Lightly Active"){
                            bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
                            tdee=bmr*1.375;

                        }

                        else if(itemActivity=="Moderalety Active"){
                            bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
                            tdee=bmr*1.55;
                        }

                        else if(itemActivity=="Very Active"){
                            bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
                            tdee=bmr*1.725;
                        }

                        else if(itemActivity=="Extremely Active"){
                            bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
                            tdee=bmr*1.9;
                        }




                    }

                    else if(itemGender=="Female") {

                        Weight=Integer.parseInt(etWeight.getText().toString());
                        Height=Integer.parseInt(etHeight.getText().toString());
                        Age=Integer.parseInt(etAge.getText().toString());

                        if(itemActivity=="Sedentary"){

                            bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
                            tdee=bmr*1.2;


                        }

                        else if(itemActivity=="Lightly Active"){
                            bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
                            tdee=bmr*1.375;

                        }

                        else if(itemActivity=="Moderalety Active"){
                            bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
                            tdee=bmr*1.55;
                        }

                        else if(itemActivity=="Very Active"){
                            bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
                            tdee=bmr*1.725;
                        }

                        else if(itemActivity=="Extremely Active"){
                            bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
                            tdee=bmr*1.9;
                        }


                    }

                    result=Double.toString(tdee);

                    tvResult.setText(result);




                }
            });


}


@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

日志猫:

  11-28 17:20:05.501: E/AndroidRuntime(1455): FATAL EXCEPTION: main
11-28 17:20:05.501: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.os.Looper.loop(Looper.java:137)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at dalvik.system.NativeStart.main(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455):     at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:20:05.501: E/AndroidRuntime(1455):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:20:05.501: E/AndroidRuntime(1455):     ... 11 more
11-28 17:24:42.341: D/AndroidRuntime(1507): Shutting down VM
11-28 17:24:42.341: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-28 17:24:42.371: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-28 17:24:42.371: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.os.Looper.loop(Looper.java:137)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at dalvik.system.NativeStart.main(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507):     at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:70)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:24:42.371: E/AndroidRuntime(1507):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:24:42.371: E/AndroidRuntime(1507):     ... 11 more

最佳答案

如果 btnCalculate 为空,您应该在执行 onclickListener 之前初始化:

 btnCalculate = (Button)findViewById(R.id.btnCalculate); //your id

另请注意,您正在初始化 dataAdapter2,但实际上您正在使用 dataAdapert,并且 list2 没有任何元素,因为您声明了它,但您始终填充 list1

关于java.lang.runtimeException 无法启动 Activity componentinfo java.lang.nullpointerException android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196707/

相关文章:

eclipse - 加快 PDE 编辑-编译-调试周期

java - 出于测试目的将 JSON 对象响应传递给 TextView 时出错

java - 如何通过远程方法发送json-rpc http post请求并在java中传递加密参数

安卓链接器 : undefined reference to bsd_signal

android - 一个 ExpandableListView 的多种布局

java - Android/Eclipse 代码错误

java - 无法在我的项目中使用 @Slf4j 注释,因为无法解析为类型,但在另一个项目中效果很好

java - 如何创建 WSDL 文件来返回 ArrayList?

java - Spring JPA/hibernate : use multiple projections on same query interface

android - 如何使用 MLT 从给定的视频文件中分离音频和视频?