java - 从编辑文本检索地名到字符串时,来自地理编码器类的 NullPointerException

标签 java android

下面是我的代码。在将地名捕获到字符串中读取地名时,我收到了 NullPointerException 异常。请帮我解决这个问题。

异常:

DalvikVM[localhost:8620]    
    Thread [<1> main] (Running) 
    Thread [<8> Binder Thread #2] (Running) 
    Thread [<7> Binder Thread #1] (Running) 
    Daemon Thread [<10> RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@405141c8] (Running)  
    Thread [<9> Thread-13] (Suspended (exception NullPointerException)) 
        TravellogActivity$2$1.run() line: 165   

代码:

public boolean onOptionsItemSelected(MenuItem m)
    {

        switch(m.getItemId())
        {
        case R.id.setroute:
            LayoutInflater li= LayoutInflater.from(this);
            View textEntryView = li.inflate(R.layout.alertbox, null);

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

          splc = (EditText) findViewById(R.id.strtplace);
            dialog.setPositiveButton("Ok", new OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    pd = ProgressDialog.show(TravellogActivity.this, "Working..", "Searching your address", true, false);
                    //Show a progress dialog


                    searchAdress = new Thread(){
                    public void run(){
                            String addressInput = splc.getText().toString(); // <-- here Iam getting exception null pointer)
                            try {
                                foundAdresses = gc.getFromLocationName(addressInput, 5); // Search addresses
                                Thread.sleep(1500);
                            } catch (Exception e) {
                                // @todo: Show error message
                            }
                            showAdressResults.sendEmptyMessage(0);                  
                        }
                    };
                    searchAdress.start();

                }
            });
           dialog.show();
        }
        return true;
        }      

 private Handler showAdressResults = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    pd.dismiss();

                    if (foundAdresses.size() == 0) { // if no address found,
                        // display an error
                        Dialog locationError = new AlertDialog.Builder(
                                TravellogActivity.this).setIcon(0).setTitle(
                                "Error").setPositiveButton(R.string.ok, null)
                                .setMessage(
                                        "Sorry, your address doesn't exist.")
                                .create();
                        locationError.show();

                    } else { // else display address on map
                        for (int i = 0; i < foundAdresses.size(); ++i) {
                            // Save results as Longitude and Latitude
                            // @todo: if more than one result, then show a
                            // select-list
                            Address x = foundAdresses.get(i);
                            latit = x.getLatitude();
                            longi = x.getLongitude();
                        }
                        navigateToLocation((lat * 1000000), (lon * 1000000),myMapView); // display the found address
                    };  

        return  ;

    }

                private void navigateToLocation(double latitude, double longitude,
                        MapView myMapView) {
                    // TODO Auto-generated method stub
                    GeoPoint p = new GeoPoint((int) latitude, (int) longitude); // new
                    MC.animateTo(p);
                    myMapView.displayZoomControls(true); // display Zoom (seems that it doesn't
                    // work yet)
                    MapController mc = myMapView.getController();
                    mc.animateTo(p); // move map to the given point
                    int zoomlevel = myMapView.getMaxZoomLevel(); // detect maximum zoom level
                    mc.setZoom(zoomlevel - 1); // zoom
                    myMapView.setSatellite(false); // display only "normal" mapview
        }

最佳答案

错误可能在这一行:

splc = (EditText) findViewById(R.id.strtplace);

替换为

splc = (EditText)   textEntryView.findViewById(R.id.strtplace);

因为您要扩充该 View ,并且 EditText View 必须位于该 textEntryView 中。

关于java - 从编辑文本检索地名到字符串时,来自地理编码器类的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6491411/

相关文章:

java - 如何阻止构造函数在派生类中尝试使用私有(private)但不起作用的java继承中覆盖

java - 夸库斯 CDI : Interceptor has no bindings

android - 增强现实 3D 模型渲染

java - 更有效地读取按字母顺序排序的文本文件

java - Webfilter 只能在第二个响应中登录用户(记住我)

java - 我很难启动我的 IntellijIDEA 2019.2

java - 这个方法内部发生了什么?

java - intellij android hello world 不起作用 "Error loading preferences"

android - 如何将删除项目选项添加到 Spinner 下拉列表

android - 以编程方式删除 Android SMS