相关疑难解决方法(0)

如何以编程方式为视图分配ID?

在XML文件中,我们可以为视图分配ID android:id="@+id/something",然后调用findViewById(),但在以编程方式创建视图时,如何分配ID?

我认为setId()与默认分配不同.setId()是额外的.

任何人都可以纠正我吗?

android android-view findviewbyid android-resources android-identifiers

195
推荐指数
3
解决办法
14万
查看次数

getSupportFragmentManager().findFragmentById()始终返回null

我正试图在我的应用程序中显示GoogleMap,但在此任务开始时我遇到了问题.这条线:

SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

始终返回null.

我尝试了很多东西,但没有任何作用.你能帮我找到问题吗?

这是我的代码:

public class MapSectionFragment extends Fragment implements
    ConnectionCallbacks,
    OnConnectionFailedListener,
    LocationListener,
    OnMyLocationButtonClickListener,
    OnMapReadyCallback{

    private GoogleMap map;
    Location location;
    GoogleApiClient mGoogleApiClient;

    private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000)         // 5 seconds
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    private void createMapView(){
        try{
            //here it comes
            SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

            map = mapFrag.getMap(); //throws NullPointerException
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_section_map,
            container, false);

        createMapView(); …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments google-maps-android-api-2

1
推荐指数
1
解决办法
4907
查看次数