0

在我的应用程序中,有两个AutoCompleteTextView和一个Button来计算某个操作。我正在设置对Button两者的响应AutoCompleteTextView,如果两者都AutoCompleteTextView被填充,那么只有这样Button才会出现。

这样做我得到一个空指针异常。程序如下:

boolean isLocation=false;
boolean isDestination=false;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location=(AutoCompleteTextView)findViewById(R.id.locale);
        location.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
        location.setOnItemClickListener(new OnItemClickListener()

    {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            // TODO Auto-generated method stub
            place1 = (Places) adapterView.getItemAtPosition(position);
            location.setText(place1.description);
            location.clearFocus();
            destination.requestFocus();
            Log.d("AutoCompleteTask", location.getText().toString());
                }
    });

    location.addTextChangedListener(new TextWatcher()
    {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            fillLocation=true;
        }

    });


    destination = (AutoCompleteTextView)findViewById(R.id.destination);
    destination.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
    destination.setOnItemClickListener(new OnItemClickListener() 
    {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                // TODO Auto-generated method stub
            place2 = (Places) adapterView.getItemAtPosition(position);
            destination.setText(place2.description);
            destination.clearFocus();
            calculate.requestFocus();               
            Log.d("AutoCompleteTask2", destination.getText().toString());
                }
    });

    destination.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            fillDestination=true;
        }
    });

    if(fillLocation==true || fillDestination==true )
    {
        calculate.setVisibility(View.VISIBLE);
    }
    else
    {
        calculate.setVisibility(View.INVISIBLE);
    }


    calculate = (Button)findViewById(R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            if(location.getText().toString().length()<1 || destination.getText().toString().length()<1) 
            {
                Toast.makeText(getApplicationContext(), "Give values before placing a query", Toast.LENGTH_LONG).show();
            }
            else{
                @SuppressWarnings("deprecation")
                String url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="+URLEncoder.encode(location.getText().toString())+"&destinations="+URLEncoder.encode(destination.getText().toString())+"&unit=metric&mode=driving&sensor=false";
                new ReadDistanceJSONFeedTask().execute(url);
                }
            }
        });

作为开发应用程序的新手,这个空指针异常是什么?有人可以帮忙吗?

4

3 回答 3

0

当您的代码中的某些内容返回 null 时,会发生 Null 指针异常

例如,如果您有:

int count;
String[] biz = {"hand","is","hammer"}

然后你尝试这样做:

String isnt = biz[count];

如果你没有像这样初始化变量计数:

count = 1;

然后

String isnt = biz[count]; 

会抛出空指针异常,因为没有设置任何计数

于 2013-04-10T03:47:16.163 回答
0

因为这个,你得到了一个NPE: -

if(fillLocation==true || fillDestination==true )
{
    calculate.setVisibility(View.VISIBLE); // NPE here
}
else
{
    calculate.setVisibility(View.INVISIBLE); // NPE here
}
calculate = (Button)findViewById(R.id.calculate);

您已经尝试设置计算按钮的可见性,甚至在初始化它之前。移动calculate = (Button)findViewById(R.id.calculate);到块上方,if-else它应该可以工作。

于 2013-04-10T03:49:37.440 回答
0

它正在抛出NullPointerException,因为您calculate = (Button)findViewById(R.id.calculate);不可见将其放在您的onCreate()方法中setContentView()

于 2013-04-10T04:01:30.977 回答