【问题标题】:App crashing on setText in InfoWindow应用程序在 InfoWindow 中的 setText 上崩溃
【发布时间】:2017-06-11 00:47:57
【问题描述】:

我的 Android 应用程序中的 InfoWindow 有这个问题。当我运行这段代码时:

@Override
public View getInfoWindow(Marker marker) {

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.lkonty, null);

    TextView txtName= (TextView) findViewById(R.id.txtName);
    txtName.setText(marker.getTitle());
    return view;

应用程序在运行 setText 方法时崩溃。我通过添加这行代码找到了解决方案:

setContentView(R.layout.lkonty);

应用程序不再崩溃,但现在 InfoWindow 填满了整个屏幕,我不知道如何更改。

【问题讨论】:

    标签: android android-layout infowindow settext


    【解决方案1】:

    试试这个

     View v = LayoutInflater.from(parent.getContext())
     .inflate(R.layout.lkonty,parent,false);
    
    
    TextView txtName= (TextView) view.findViewById(R.id.txtName);
    
    txtName.setText(marker.getTitle());
    

    【讨论】:

      【解决方案2】:

      应用程序崩溃,因为您的 textview txtName 为空。 你必须通过view 找到你的txtName 你膨胀了。 它会是这样的:

      @Override
      public View getInfoWindow(Marker marker) {
      
          LayoutInflater inflater = getLayoutInflater();
          View view = inflater.inflate(R.layout.lkonty, null);
      
          TextView txtName= (TextView) view.findViewById(R.id.txtName);
          txtName.setText(marker.getTitle());
          return view;
      }
      

      【讨论】:

        【解决方案3】:

        我认为您在绑定 textview 时忘记使用 view

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

        在使用inflater时你必须使用view

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多