【问题标题】:While using custom InfoWindow getting NullPointerException使用自定义 InfoWindow 时获取 NullPointerException
【发布时间】:2014-02-20 04:50:50
【问题描述】:

我在我的应用程序中使用自定义信息窗口。我有两个与之相关的问题。

  1. 是否可以在 InfoWindow 中制作多行 sn-p

假设我正在添加一些数据,假设我需要在单击标记时显示 LATTITUDE 和 LONGITUDE。通常,如果您在 sn-p 中设置这些值,它们会出现在同一行。我想在两条线上展示它们。有可能吗

我在 Google 上搜索了多行 sn-p 并找不到任何有用的信息,因此想出了自定义 InfoWindow 的想法。

2.下面显示的是我的自定义信息窗口,它给了我 NullPointerException

从 try-catch 循环中获取异常。我遵循了一些在谷歌搜索时发现的代码,但它不起作用。

 private class MyCustomInfoWindow implements InfoWindowAdapter{


    private View view;

    public MyCustomInfoWindow(){

    }

    @Override
    public View getInfoContents(Marker markr) {
        // TODO Auto-generated method stub


        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        // TODO Auto-generated method stub

        view = getLayoutInflater().inflate(R.layout.mycustom_infowindow, null);
        TextView marker_heading = (TextView)findViewById(R.id.marker_heading);
        try{

            String m_heading = marker.getId();
            marker_heading.setText(m_heading);

        }
        catch(Exception exx){
            Toast.makeText(getApplicationContext(), "getInfoWindow Error is  " + marker_heading.length(), Toast.LENGTH_LONG).show();

        }
        return view;
    }



}    

我的习惯

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">    

  <TextView 
    android:id="@+id/marker_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="serif"
    android:textStyle="bold" 
    />    

</LinearLayout>

使用谷歌地图 V2

谁能告诉我哪里出错了。

【问题讨论】:

  • 对于Is it possible to make multiline snippet in InfoWindow,您可以添加单独的 TextView 以在单独的行中显示 LATTITUDE 和 LONGITUDE,或者如果您只想在 TextView 上显示,请使用换行符
  • 关于编辑,当有人说googlinggoogled 时,没有理由更改为google,因为它会混淆含义。代码编辑更合适。

标签: android google-maps customization infowindow


【解决方案1】:

使用

TextView marker_heading = (TextView)view.findViewById(R.id.marker_heading);

初始化marker_heading TextView 实例,因为TextViewmycustom_infowindow 布局内

​​>

【讨论】:

    【解决方案2】:

    你应该替换这个

      TextView marker_heading = (TextView)findViewById(R.id.marker_heading);
    

      TextView marker_heading = (TextView)view.findViewById(R.id.marker_heading);
    

    【讨论】:

      【解决方案3】:

      使用

      TextView marker_heading = (TextView)view.findViewById(R.id.marker_heading); 
      

      你必须传递你的 View 的对象作为参考才能找到 TextView 的 id

      【讨论】:

        猜你喜欢
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 2018-04-01
        • 2017-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多