gpt4 book ai didi

java - 当我用抽屉导航实现它时,SupportMapFragment 为空

转载 作者:行者123 更新时间:2023-12-02 05:13:30 25 4
gpt4 key购买 nike

我恳请您提供任何帮助,我已经为这个问题伤透了两天了,在解决这个问题之前我无法继续:/

嗯,我正在尝试使用抽屉导航实现 GoogleMap,并且我勉强做到了这一点,但效果不太好。

这是我的类和 xml-s,请记住它们中的大部分是自动生成的。

MapFragment.java:

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.support.v4.app.Fragment;
import android.widget.Toast;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link MapFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link MapFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MapFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

// TODO: Rename and change types of parameters


private OnFragmentInteractionListener mListener;

private GoogleMap gMap;
private Double lat, lng;

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MapFragment.
*/
// TODO: Rename and change types and number of parameters
public static MapFragment newInstance() {
MapFragment fragment = new MapFragment();
Bundle args = new Bundle();
args.putString("Map", "map");
fragment.setArguments(args);
return fragment;
}

public MapFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}*/

setUpMapIfNeeded();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_map, container, false);

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onResume(){
super.onResume();
setUpMapIfNeeded();
}


@Override
public void onDetach() {
super.onDetach();
mListener = null;
}


/*
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
*/

public void setUpMapIfNeeded(){

try {
if (gMap == null)
gMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
catch (NullPointerException e) {
Toast.makeText(getActivity(), "Error with retrieving map", Toast.LENGTH_LONG).show();
}

if (gMap != null) {
setUpMap();
}

}
public void setUpMap() {

Toast.makeText(getActivity(),"Setting up map", Toast.LENGTH_LONG);
gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
gMap.getUiSettings().setMyLocationButtonEnabled(true);
gMap.setMyLocationEnabled(true);
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}


}

fragment_map.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

</RelativeLayout>

MainActivity.java:

import android.app.Activity;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;


public class MainActivity extends FragmentActivity
implements MapFragment.OnFragmentInteractionListener,StatsFragment.OnFragmentInteractionListener,NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();



// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();



switch (position) {
case 0: {
mTitle = getString(R.string.title_section1);
try {
fragmentManager.beginTransaction().replace(R.id.container, MapFragment.newInstance()).commit();
break;
}
catch (NullPointerException e) {Toast.makeText(this,"Error retrieving map fragment",Toast.LENGTH_LONG);}
}



case 1: {
mTitle = getString(R.string.title_section2);
fragmentManager.beginTransaction()
.replace(R.id.container, StatsFragment.newInstance())
.commit();
break;
}
}




}

public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
}
}

public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}

@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_about) {
openHelpDialog();
return true;
}

return super.onOptionsItemSelected(item);
}


public void openHelpDialog(){

new AlertDialog.Builder(this)
.setTitle("About")
.setMessage("Pocket Buddy V1.0 \nAuthor : Milos Matic")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();


}

@Override
public void onFragmentInteraction(Uri uri) {

}
}

Android list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aaaaaaaaaaa" >


<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="I'm a using correct key"/>

<activity
android:name="com.shomi.pocketbuddy.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

嗯,主要问题是每次切换到 MapFragment 时我都没有获得 GoogleMap 对象,我设法用 try-catch block 绕过它并且它可以工作,但它不允许我在 map 上设置任何首选项.

例如,setUpMap() 永远不会被调用。

我尝试重新排列我的代码好几次,它仍然是一样的。

我希望能够保存 fragment 状态,然后自由切换而不丢失我的数据(我想根据移动在 map 上绘制,所以不能每次都从头开始绘制)但我不能这样做直到我解决这个问题。

我认为问题在于 Gmap 对象的准备速度不够快,但我不知道如何解决这个问题:/

我的设备是 Nexus 5,运行 Android 5.0

感谢您的每一点帮助。

最佳答案

而不是

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

使用

gMap = ((SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map)).getMap();

关于java - 当我用抽屉导航实现它时,SupportMapFragment 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com