Android LayoutInflater获取EditText数据

时间:2014-07-24 20:51:16

标签: java android

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

但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();
}

1 个答案:

答案 0 :(得分:2)

看起来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);