我从 String placeName = placeText.getText().toString(); 收到空指针异常

发布于 2024-11-16 12:41:02 字数 3038 浏览 1 评论 0原文

您好,想从编辑文本中获取地名并在地图上标记,这是我的代码,其中出现空指针异常,请帮助我应该做什么以及哪里出错了。

因为我从对话框中的编辑文本字段获取地名。

View layout = View.inflate(this, R.layout.alertbox, null);

            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Enter the places");
            dialog.setView(layout);

            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {



                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // TODO Auto-generated method stub


                     EditText placeText = (EditText) findViewById(R.id.strtplace);          
                             String placeName = placeText.getText().toString();
//                              
  Break from execution if the user has not entered anything in the field
                        if(placeName.compareTo("")==0) 
                        numberOptions = 5;
                        String [] optionArray = new String[numberOptions];
                        Geocoder gcoder = new Geocoder(TravellogActivity.this);  

                        try{
                            List<Address> results = gcoder.getFromLocationName(placeName,numberOptions);
                            Iterator<Address> locations = results.iterator();
                            String raw = "\nRaw String:\n";
                            String country;
                            int opCount = 0;
                            while(locations.hasNext()){
                                Address location = locations.next();
                                lat = location.getLatitude();
                                lon = location.getLongitude();
                                country = location.getCountryName();
                                if(country == null) {
                                    country = "";
                                } else {
                                    country =  ", "+country;
                                }
                                raw += location+"\n";
                                optionArray[opCount] = location.getAddressLine(0)+", "+location.getAddressLine(1)+country+"\n";
                                opCount ++;
                            }
                            Log.i("Location-List", raw);
                            Log.i("Location-List","\nOptions:\n");
                            for(int i=0; i<opCount; i++){
                                Log.i("Location-List","("+(i+1)+") "+optionArray[i]);
                            }

                        } catch (IOException e){
                            Log.e("Geocoder", "I/O Failure; is network available?",e);
                        }           

                        p = new GeoPoint(latE6, lonE6);
                        myMC.animateTo(p); 


                    }                       
                });

            dialog.show();
            break;

            }



        return false  ;

    }

Hi want to get the place name from the edit text and mark on map here is my code where i got null pointer exception please help me what i should do and where i am going wrong.

as I am getting the place name from the edit text field in dialog box.

View layout = View.inflate(this, R.layout.alertbox, null);

            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Enter the places");
            dialog.setView(layout);

            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {



                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // TODO Auto-generated method stub


                     EditText placeText = (EditText) findViewById(R.id.strtplace);          
                             String placeName = placeText.getText().toString();
//                              
  Break from execution if the user has not entered anything in the field
                        if(placeName.compareTo("")==0) 
                        numberOptions = 5;
                        String [] optionArray = new String[numberOptions];
                        Geocoder gcoder = new Geocoder(TravellogActivity.this);  

                        try{
                            List<Address> results = gcoder.getFromLocationName(placeName,numberOptions);
                            Iterator<Address> locations = results.iterator();
                            String raw = "\nRaw String:\n";
                            String country;
                            int opCount = 0;
                            while(locations.hasNext()){
                                Address location = locations.next();
                                lat = location.getLatitude();
                                lon = location.getLongitude();
                                country = location.getCountryName();
                                if(country == null) {
                                    country = "";
                                } else {
                                    country =  ", "+country;
                                }
                                raw += location+"\n";
                                optionArray[opCount] = location.getAddressLine(0)+", "+location.getAddressLine(1)+country+"\n";
                                opCount ++;
                            }
                            Log.i("Location-List", raw);
                            Log.i("Location-List","\nOptions:\n");
                            for(int i=0; i<opCount; i++){
                                Log.i("Location-List","("+(i+1)+") "+optionArray[i]);
                            }

                        } catch (IOException e){
                            Log.e("Geocoder", "I/O Failure; is network available?",e);
                        }           

                        p = new GeoPoint(latE6, lonE6);
                        myMC.animateTo(p); 


                    }                       
                });

            dialog.show();
            break;

            }



        return false  ;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聽兲甴掵 2024-11-23 12:41:02

NullPointerExceptions 是最容易查找和调试的异常。

如果您在此行遇到 NullPointerException

String placeName = placeText.getText().toString();

唯一可以为 null 的是 placeText 变量,或 placeText.getText()

您需要找出原因它为空。由于您的 placeText 处于警报状态,因此您应该使用layout.findViewById 从布局中获取它。当您从错误的位置获取它时,它将为 null,从而导致 NullPointerException

理论上,getText()也可以返回null,但是Android SDK中当前的实现在没有填写任何内容时会返回一个空字符串,因此不能为null。

NullPointerExceptions are the most easy exceptions to find and debug.

If you're getting a NullPointerException on this line :

String placeName = placeText.getText().toString();

The only thing that can be null is the placeText variable, or the placeText.getText()

You need to figure out why it is null. As your placeText is on the alert, so you should fetch it from the layout using layout.findViewById. As your fetching it from the wrong place, it will be null, causing the NullPointerException.

in theory, getText() could also return null, but the current implementation in the Android SDK will return an empty string when nothing is filled in, so that can't be null.

冰魂雪魄 2024-11-23 12:41:02

使用 EditText placeText = (EditText) layout.findViewById(R.id.strtplace); 而不是 EditText placeText = (EditText) findViewById(R.id.strtplace);

use EditText placeText = (EditText) layout.findViewById(R.id.strtplace); intead of EditText placeText = (EditText) findViewById(R.id.strtplace);

烟燃烟灭 2024-11-23 12:41:02

这是您需要做的更改:
//视图和editext都应该声明为final,并且findViewById方法需要一个布局来调用它

  • final View布局 = View.inflate(this, R.layout.alertbox, null);
    • final EditText placeText = (EditText)layout.findViewById(R.id.strtplace);

heres the changes that you need to do:
//both the view and the editext should be declared as final and the findViewById method needs a layout on which it is to be called

  • final View layout = View.inflate(this, R.layout.alertbox, null);
    • final EditText placeText = (EditText) layout.findViewById(R.id.strtplace);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文