我用2个标签创建了主要活动,我正在尝试在第一个标签片段上实现地图。应用程序显示市场位于(0,0)的默认地图,但是当我点击按钮从文本框中查找新位置时,它会崩溃。 OneFragment java代码:
public class OneFragment extends Fragment implements OnMapReadyCallback, View.OnClickListener{ // implements OnMapReadyCallback, View.OnClickListener
private GoogleMap mMap;
private EditText mOriginPlace;
private Button mSearchOriginButton;
private View mView;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onMapReady(GoogleMap googleMap) { //when map service started
mMap = googleMap;
LatLng hcmus = new LatLng(10.762963, 106.682394); //create position with latitude and longitude
mMap.addMarker(new MarkerOptions()
.position(hcmus)
.title("Title"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(hcmus, 10));
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpMapIfNeeded();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_one, container, false);
mSearchOriginButton = (Button) mView.findViewById(R.id.searchOriginButton);
mSearchOriginButton.setOnClickListener(this);
return mView;
}
@Override
public void onClick(View v) {
mOriginPlace = (EditText) mView.findViewById(R.id.originPlaceText);
String placeOriginText = mOriginPlace.getText().toString();
List<Address> addressList = null;
if (placeOriginText != null || !placeOriginText.equals("")) {
Geocoder geocoder = new Geocoder(getActivity());
try {
addressList = geocoder.getFromLocationName(placeOriginText, 1);
} catch (IOException e) {
e.printStackTrace();
}
if(addressList.size() > 0) {
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}
} else {
Toast.makeText(getActivity(), "Please enter origin address!", Toast.LENGTH_SHORT).show();
}
}
public void setUpMapIfNeeded() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.originPlaceMap);
mapFragment.getMapAsync(this); //it'll start map service, getMapAsync(), чтобы установить обратный вызов для фрагмента
}
}
}
从logcat登录:
04-20 18:44:11.154 6485-6485/com.itshareplus.googlemapdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.itshareplus.googlemapdemo, PID: 6485
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at fragments.OneFragment.onClick(OneFragment.java:94)
at android.view.View.performClick(View.java:4909)
at android.view.View$PerformClick.run(View.java:20390)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5848)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
04-20 18:44:11.328 6485-6485/com.itshareplus.googlemapdemo I/Process: Sending signal. PID: 6485 SIG: 9
答案 0 :(得分:0)
由于您没有添加崩溃日志,因此很难为您的问题提供解决方案。您可以确保更新地图视图是否发生在主UI线程上(使用主循环器)。取决于您的屏幕导航方式有时候getActivity将为null确保也是如此。如果您添加崩溃日志,我可以为您提供更多帮助
答案 1 :(得分:0)
地址有时间从地理编码器接收结果,所以尝试将整个onclick方法放在try catch块下。