【问题标题】:java.lang.runtimeexception unable to start activity componentinfo java.lang.nullpointerexception androidjava.lang.runtimeexception 无法启动活动组件信息 java.lang.nullpointerexception android
【发布时间】:2014-11-28 22:30:37
【问题描述】:

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

代码如下:

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

【问题讨论】:

  • 第 67 行是什么? Caused by: java.lang.NullPointerException at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67) 另外,第 70 行。(如果你告诉我们 67,我想我们可以找到 70)
  • 关于代码?第 67 行是:dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);第 70 行是: btnCalculate.setOnClickListener(new OnClickListener() {
  • List2 是一个空列表吗? List list2 = new ArrayList(); list.add("久坐");
  • 哦,谢谢你注意到这一点,我也解决了,谢谢鬃毛:D,这就是我制作复制粘贴的结果呵呵

标签: java android eclipse nullpointerexception runtimeexception


【解决方案1】:

你的 btnCalculate 是 null,你应该在执行 onclickListener 之前初始化:

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

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

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2023-03-25
    相关资源
    最近更新 更多