【问题标题】:Mapview fragment type mismatch errorMapview 片段类型不匹配错误
【发布时间】:2018-02-11 17:19:23
【问题描述】:

我正在尝试define 带有mapview 的片段。 我的xml 文件是:

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--android:background="@android:color/holo_orange_dark" >-->

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

java 代码是:

FirstFragment.java(从谷歌的仓库复制)

public class FirstFragment extends FragmentActivity
        implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_first);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we
     * just add a marker near Africa.
     */
    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

现在,我将这个片段称为我的 MainActivity

public class MainActivity extends AppCompatActivity{
 ...
    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch (pos) {

                case 0:
                    return FirstFragment.onMapReady();

                default:
                    return ThirdFragment.newInstance("ThirdFragment, Default");
            }
        }

        @Override
        public int getCount() {
            return 5;
        }
    }
}

这给了我编译时错误:

错误:(105, 41) 错误:FirstFragment 类中的方法 onMapReady 不能 应用于给定类型;必需:找到 GoogleMap:无参数 原因:实际参数列表和形式参数列表的长度不同

我在这里出了什么问题?活动的论点有一部分是显而易见的,我仍然需要弄清楚。但是错误是什么:

不能从静态引用非静态方法 OnMapReady(...) 上下文

请帮忙。

【问题讨论】:

    标签: java android


    【解决方案1】:

    不能从静态上下文引用非静态方法 OnMapReady(...)

    你需要使用FirstFragment的对象来调用它的方法onMapReady(),因为onMapReady()是非静态的。

    【讨论】:

    • 我试过了:FirstFragment newMapFragment = new FirstFragment(); newMapFragment.onMapReady(location); 出现无法解决 onMapReady() 的错误
    • @BaRud 您应该在实现中按预期传递 GoogleMap 对象:
    • 是的,但是在参数传递之前我就遇到了错误(我的意思是在 android studio 的代码检查器中)。你能告诉我该怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2018-06-08
    相关资源
    最近更新 更多