我从字符串placeName = placeText.getText().toString();获得空指针异常

我从字符串placeName = placeText.getText().toString();获得空指针异常,第1张

概述嗨,您想从编辑文本中获取地名,并在地图上进行标记吗?这是我的代码,在此代码中我出现了空指针异常,请帮助我该怎么办以及我要去哪里.因为我是从对话框中的编辑文本字段获取地名的.Viewlayout=View.inflate(this,R.layout.alertbox,null);AlertDialog.Builderd

嗨,您想从编辑文本中获取地名,并在地图上进行标记吗?这是我的代码,在此代码中我出现了空指针异常,请帮助我该怎么办以及我要去哪里.

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

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

解决方法:

使用EditText placeText =(EditText)layout.findVIEwByID(R.ID.strtplace);插入EditText placeText =(EditText)findVIEwByID(R.ID.strtplace);

总结

以上是内存溢出为你收集整理的我从字符串placeName = placeText.getText().toString();获得空指针异常全部内容,希望文章能够帮你解决我从字符串placeName = placeText.getText().toString();获得空指针异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094428.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存