这是我的GoogleMapAPI,希望用户可以先输入Marker信息,然后使用它来做标记。

但是LogCat总是说“ String MarkerInfo = markerInfo.getText()。toString();”失败。

抱歉,我是编码的新手。请帮我。

public void onMapLongClick(final LatLng point) {

    AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
    builder1.setTitle(R.string.funtion);
    builder1.setItems(choice, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,int selectedItem) {
               Toast.makeText(MainActivity.this,"You Select Letter "+choice[selectedItem],Toast.LENGTH_SHORT).show();
                  dialog.dismiss();
                  if(selectedItem==1){
                      AlertDialog.Builder builder2;
                      Context mContext = MainActivity.this;
                      LayoutInflater inflater = getLayoutInflater();
                      builder2 = new AlertDialog.Builder(mContext);
                      markerInfo = (EditText)findViewById(R.id.markerInfo);
                      builder2.setView(inflater.inflate(R.layout.markerinfo, null))
                               .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface marker, int id) {
                                       String MarkerInfo = markerInfo.getText().toString();
                                       map.addMarker(new MarkerOptions()
                                       .position(point)
                                       .title(MarkerInfo)
                                       .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                                    }
                               })
                               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface marker, int id) {
                                   }
                               });
                      builder2.setTitle(R.string.typeinfo);
                      AlertDialog marker = builder2.create();
                      marker.show();
                  }
              }
          });
    AlertDialog alert = builder1.create();
    alert.show();
}

最佳答案

看起来R.id.markerInfo被声明为insido markerinfo.xml,因此您必须使用markerinfo.xml的“膨胀”版本来检索EditText

View view = inflater.inflate(R.layout.markerinfo, null);
markerInfo = (EditText)view.findViewById(R.id.markerInfo);
builder2.setView(view);

08-17 10:20