[英]How to add map to a tabbed activity (Android)
I have a tabbed activity which uses a SectionsPageAdapter. 我有一个使用SectionsPageAdapter的选项卡式活动。 There are two tabs, each uses a different fragment, one of which is a google map.
有两个标签,每个标签使用不同的片段,其中一个是google map。 When I attempt to get the
SupportMapFragment
from the MapFragment it returns null. 当我尝试从MapFragment获取
SupportMapFragment
时,它返回null。
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
I have also tried: 我也尝试过:
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
If I create a new GoogleMapsActivity it works just fine, that confirms that it has nothing to do with my key. 如果我创建一个新的GoogleMapsActivity,它就可以正常工作,这表明它与我的密钥无关。 the fragment with id
map
exists. ID
map
片段存在。
MainActivity.java MainActivity.java
package com.example.frias19o.trackthem2;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MapFragment.java MapFragment.java
package com.example.frias19o.trackthem2;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONException;
import org.json.JSONObject;
/********************************************************************
* Fragment for Map
*********************************************************************/
public class MapFragment extends Fragment implements OnMapReadyCallback {
private static final String LOGTAG = "MapFragment";
private static final String ARG_SECTION_NUMBER = "section_number";
public static GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;
public MapFragment() {
}
public static MapFragment newInstance(int sectionNumber) {
MapFragment fragment = new MapFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
Toast.makeText(getContext(), "fragment_map", Toast.LENGTH_SHORT).show();
rootView = inflater.inflate(R.layout.activity_maps, container, false);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); // it brakes here because mapFragment is null
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Log.v(LOGTAG, "map is ready");
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
Log.v(LOGTAG, "Added Sydney");
}
}
SectionsPagerAdapter.java SectionsPagerAdapter.java
package com.example.frias19o.trackthem2;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/***********************************************************************
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
**********************************************************************/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a ClusterFragment (defined as a static inner class below).
Fragment fragment = null;
switch (position) {
case 0:
fragment = MapFragment.newInstance(position + 1);
break;
case 1:
fragment = ClusterFragment.newInstance(position + 1);
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
}
return null;
}
}
note: The code on the link provided by CoomonsWare in the first comment on the question, led me to a solution. 注意:CoomonsWare在对该问题的第一条评论中提供的链接上的代码使我找到了解决方案。
As pointed out by CommonsWare, the solution was making MapFragment
extend SupportMapFragment
, that way getMapAsync(this);
正如CommonsWare指出的那样,解决方案是使
MapFragment
扩展SupportMapFragment
,即getMapAsync(this);
can be called directly (without having to access the FragmentManager). 可以直接调用(无需访问FragmentManager)。 Then return an instance of this MapFragment in the
getItem()
of SectionsPagerAdapter. 然后在SectionsPagerAdapter的
getItem()
中返回此MapFragment的实例。 Note that there should NOT be a call/override for onCreateView()
, it caused java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ei.bz.o()' on a null object reference
请注意,不应存在对
onCreateView()
的调用/重写,它导致java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ei.bz.o()' on a null object reference
Final Product: 最终产品:
MapFragment.java MapFragment.java
package com.example.frias19o.trackthem2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/********************************************************************
* Fragment for Map
*********************************************************************/
public class MapFragment extends SupportMapFragment implements OnMapReadyCallback {
private static final String LOGTAG = "MapFragment";
private GoogleMap mMap;
public MapFragment() {
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Log.v(LOGTAG, "map is ready");
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
Log.v(LOGTAG, "Added Sydney");
}
}
SectionsPagerAdapter.java SectionsPagerAdapter.java
package com.example.frias19o.trackthem2;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/***********************************************************************
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
**********************************************************************/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a ClusterFragment (defined as a static inner class below).
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MapFragment();
break;
case 1:
fragment = ClusterFragment.newInstance(position + 1);
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.