空指针异常 - Android

时间:2014-01-05 04:44:21

标签: java android eclipse nullpointerexception

我今天开始编写一个基本的温度转换应用程序for android(仍然是初学者),我似乎得到一个NullPointerException,我无法弄清楚是什么导致它。我已经尝试评论可能导致错误的代码部分,但没有运气。当我在Android设备上通过ADB运行应用程序时,应用程序在启动时崩溃。

这是一项正在进行的工作,所以请忽略任何未完成的部分(除非这是造成问题的原因)。这是代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String array_spinner1[];
    private String array_spinner2[];
    private EditText input1;
    private EditText input2;
    Spinner topSpinner;
    Spinner bottomSpinner;
    String topSpinnerVal;
    String bottomSpinnerVal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        topSpinner = (Spinner) findViewById(R.id.input1Sp);
        bottomSpinner = (Spinner) findViewById(R.id.input2Sp);
        topSpinnerVal = topSpinner.getSelectedItem().toString();
        bottomSpinnerVal = bottomSpinner.getSelectedItem().toString();
        array_spinner1=new String[3];
        array_spinner1[0]="Celsius";
        array_spinner1[1]="Fahrenheit";
        array_spinner1[2]="Kelvin";
        array_spinner2=new String[3];
        array_spinner2[0]="Celsius";
        array_spinner2[1]="Fahrenheit";
        array_spinner2[2]="Kelvin";
        ArrayAdapter adapter1 = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, array_spinner1);
        ArrayAdapter adapter2 = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, array_spinner2);
        topSpinner.setAdapter(adapter1);
        bottomSpinner.setAdapter(adapter2);
        input1 = (EditText) findViewById(R.id.input1TB);
        input1 = (EditText) findViewById(R.id.input2TB);
    }

    public void onClick(View button) {
        Double valTop = Double.parseDouble(input1.getText().toString());
        Double valBottom = Double.parseDouble(input2.getText().toString());
        switch (button.getId()) {
        case R.id.topToBottomBt:
            if (input1.getText().length() == 0){
                Toast.makeText(this, "Requested conversion is empty", Toast.LENGTH_LONG).show();
            return;
            }
            if (topSpinnerVal.equals("Celsius")) {
                if (bottomSpinnerVal.equals("Celsius")) {

                }
            }
        case R.id.bottomToTopBt:

        }
    }

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

}

Eclipse中的logcat日志说明如下:

01-05 00:01:18.687: D/AndroidRuntime(22472): Shutting down VM
01-05 00:01:18.687: W/dalvikvm(22472): threadid=1: thread exiting with uncaught exception (group=0x41e23700)
01-05 00:01:18.687: E/AndroidRuntime(22472): FATAL EXCEPTION: main
01-05 00:01:18.687: E/AndroidRuntime(22472): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tumblr.nocturnalparasites.temperatureconversion/com.tumblr.nocturnalparasites.temperatureconversion.MainActivity}: java.lang.NullPointerException
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.os.Looper.loop(Looper.java:137)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.ActivityThread.main(ActivityThread.java:5287)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at java.lang.reflect.Method.invoke(Method.java:525)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at dalvik.system.NativeStart.main(Native Method)
01-05 00:01:18.687: E/AndroidRuntime(22472): Caused by: java.lang.NullPointerException
01-05 00:01:18.687: E/AndroidRuntime(22472):    at com.tumblr.nocturnalparasites.temperatureconversion.MainActivity.onCreate(MainActivity.java:30)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.Activity.performCreate(Activity.java:5201)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-05 00:01:18.687: E/AndroidRuntime(22472):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
01-05 00:01:18.687: E/AndroidRuntime(22472):    ... 11 more

3 个答案:

答案 0 :(得分:1)

在setContentView

之后将所有这些移到onCreate
Spinner topSpinner;
Spinner bottomSpinner;
String topSpinnerVal; 
String bottomSpinnerVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topSpinner = (Spinner) findViewById(R.id.input1Sp);
bottomSpinner = (Spinner) findViewById(R.id.input2Sp);
topSpinnerVal = topSpinner.getSelectedItem().toString();
bottomSpinnerVal = bottomSpinner.getSelectedItem().toString();

findViewById查找当前膨胀布局中提到的id的视图。首先,您需要将布局的内容设置为活动,然后初始化您的视图。

编辑:

你也有

 Spinner s1 = (Spinner) findViewById(R.id.input1Sp);
 Spinner s2 = (Spinner) findViewById(R.id.input2Sp);

为什么在已经拥有以下内容时需要上述内容

topSpinner = (Spinner) findViewById(R.id.input1Sp);
bottomSpinner = (Spinner) findViewById(R.id.input2Sp);

将适配器设置为topSpinnerbottomSpinner

另外

if (topSpinnerVal == "Celsius") {

应该是

if (topSpinnerVal.equals("Celsius")) {

使用.equals来比较字符串。同样地使用.equals来比较其他地方的字符串

答案 1 :(得分:1)

我解决了这个问题。它是使用topSpinnerVal和bottomSpinnerVal字符串;在我为它们分配适配器之前,我试图从微调器中获取值。我改为将字符串赋值移动到onClick方法,该方法将在onCreate方法之后运行。

以下是工作代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String array_spinner1[];
    private String array_spinner2[];
    private EditText input1;
    private EditText input2;
    Spinner topSpinner;
    Spinner bottomSpinner;
    String topSpinnerVal;
    String bottomSpinnerVal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        topSpinner = (Spinner) findViewById(R.id.input1Sp);
        bottomSpinner = (Spinner) findViewById(R.id.input2Sp);
        array_spinner1=new String[3];
        array_spinner1[0]="Celsius";
        array_spinner1[1]="Fahrenheit";
        array_spinner1[2]="Kelvin";
        array_spinner2=new String[3];
        array_spinner2[0]="Celsius";
        array_spinner2[1]="Fahrenheit";
        array_spinner2[2]="Kelvin";
        ArrayAdapter adapter1 = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, array_spinner1);
        ArrayAdapter adapter2 = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, array_spinner2);
        topSpinner.setAdapter(adapter1);
        bottomSpinner.setAdapter(adapter2);
        input1 = (EditText) findViewById(R.id.input1TB);
        input2 = (EditText) findViewById(R.id.input2TB);
    }

    public void onClick(View button) {
        topSpinnerVal = topSpinner.getSelectedItem().toString();
        bottomSpinnerVal = bottomSpinner.getSelectedItem().toString();
        Double valTop = Double.parseDouble(input1.getText().toString());
        Double valBottom = Double.parseDouble(input2.getText().toString());
        switch (button.getId()) {
        case R.id.topToBottomBt:
            if (input1.getText().length() == 0){
                Toast.makeText(this, "Requested conversion is empty", Toast.LENGTH_LONG).show();
            return;
            }
            if (topSpinnerVal.equals("Celsius")) {
                if (bottomSpinnerVal.equals("Celsius")) {

                }
            }
        case R.id.bottomToTopBt:

        }
    }

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

}

感谢回答的用户。

答案 2 :(得分:0)

首先,看看@Raghunandan建议,你需要的所有建议。 然后,检查你的logcat以查看行,找出产生错误的视图,或者逐步调试(如果你不知道怎么做,请关注这个eclipse帮助页面:http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-stepping.htm

您的一个或多个视图会使其崩溃,因为在使用视图之前,您不会在findViewById之后检查它们是否为null。

当您找到产生错误的视图时,请在布局中查找,在正在加载的确切文件中查找您用于测试的设备大小和配置,并确保该ID存在。 (如果该id在项目的任何文件中,Eclipse将不会标记错误,但如果它不在正确的文件中,则会出现此运行时错误)