.getText()。toString()给出NullPointerException

时间:2013-03-01 06:47:27

标签: android google-maps

这是我在谷歌地图android api v2中添加标记的代码。 我的问题是我无法从EditText片段中获取String值。代码.getText()。toString()无法获取字符串值,只发送null。 请帮帮我。

    googlemap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        @Override
        public void onMapLongClick(final LatLng latlng) {
            LayoutInflater li = LayoutInflater.from(context);
            final View v = li.inflate(R.layout.alertlayout, null);

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(v);
            builder.setCancelable(false);

            builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    EditText title = (EditText) v.findViewById(R.id.ettitle);
                    EditText snippet = (EditText) v.findViewById(R.id.etsnippet);
                    String s = title.getText().toString();
                    String ss = snippet.getText().toString();
                    googlemap.addMarker(new MarkerOptions()
                            .title(s)
                            .snippet(ss)
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                            .position(latlng)
                            );
                }
            });

            //Create Negative button
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }
    });

}

2 个答案:

答案 0 :(得分:0)

中添加以下代码

onMapLongClick()

LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.alertlayout, parent, false);

答案 1 :(得分:0)

你得到空指针,因为你没有为你需要遵循的编辑文本值实现getter和setter ..

  1. 正确夸大你的布局......

    LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    

    convertView = mInflater.inflate(R.layout.alertlayout,null);

  2. 2.Crate ViewHolder类,包含该编辑文本的getter和setter。

    1. convertView.setTag(holder);

    2. 创建像holder = (ViewHolder) convertView.getTag();这样的持有者对象 现在你可以得到holder.get....();

相关问题