0

Ну, после прочтения многих примеров и многих сообщений на этом сайте я решил некоторые проблемы, и, наконец, мне удалось обработать фрагмент и работать с syncAdapter. Но ведь все эти дни пробуют много вещей Я пока не вижу карту. У меня есть пустой экран. Я пробовал несколько вещей, но он не работает для меня. Я нахожусь на стене с этим, и мне нужно продолжать двигаться с моим проектом.
Может ли кто-нибудь мне помочь, как сделать эту работу. Я бы очень признателен. Я просто прошу вас заметить все элементы, которые у меня есть, потому что я собираюсь в кругах, когда я пытался что-то изменить, другие вещи, которые работали нормально.

Это приложение имеет, как вы видите ниже: A Местоположение запрос (основная активность), которая синхронизируется при изменении и возвращает GPS Lat и Lon. После того, что она приносит фрагмента и когда-то я первый синхронизация данные с помощью GPS широчайших и Лоной отправки их следует хранить в базе данных с Схематическимбиблиотеки. SyncAdapter использует GPS lat и lon для перехода к Yelp, который делает (насколько я понимаю) вызовы Async.

fyi, я использовал такие сообщения one, но это не работает для меня.Как получить местоположение GPS и раздуть фрагмент MapView, чтобы программно добавить маркеры с SyncAdapter

я быть осторожным когда я импортировать android.support.v4.app.xxxx или простой импорт android.app.xxx, потому что портит что-то другое. Путь работает прямо сейчас, ничего не сломается, но я пока не вижу карту.

Что я пробовал:
(с результатами, как, дублированный идентификатор, NullPointer) Несколько сообщений говорят, чтобы использовать getChildFragmentManager, чтобы использовать onDestroyView, чтобы использовать класс SupportMapFragment, но ничего не работает.
Вот эти сообщения: one, two, three, four

После прочтения этих должностей я подправил код, как показано ниже с ошибкой «NullPointerException в SupportMapFragment». Я попробовал также простой MapFragment, но ничего.


Я не смогу отблагодарить вас за это.
Приветствия


Tweaked как это: (пришлось изменить minSdkVersion до 17 для этой части)


в Фрагмент

@Override 
    public void onDestroyView() 
    { 
     super.onDestroyView(); 
     Fragment fragment = (getActivity().getSupportFragmentManager().findFragmentById(R.id.thismap)); 
     FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
     ft.remove(fragment); 
     ft.commit(); 
    } 

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

     //getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this); 


       SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() 
       .findFragmentById(R.id.thismap); 
     mapFragment.getMapAsync(this); 
return null; 
} 

Попробованная это для yelp_google_map.XML (одна фрагмент, ничего другого)

<fragment android:id="@+id/thismap" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:map="http://schemas.android.com/apk/res-auto" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:context="mem.edu.joshua.MapsActivity"/> 




Всего приложение без выше подстройке
Gradle

apply plugin: 'com.android.application' 
apply plugin: 'android-apt' 
//apply plugin: 'com.google.gms.google-services' 

android { 
    compileSdkVersion 24 
    buildToolsVersion "24.0.0" 

    defaultConfig { 
     applicationId "mem.edu.joshua" 
     minSdkVersion 16 
     targetSdkVersion 24 
     versionCode 1 
     versionName "1.0" 
     multiDexEnabled true 
    } 

    dexOptions { 
     preDexLibraries = false 
     javaMaxHeapSize "4g" 
    } 

    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

    buildTypes.each { 
     it.buildConfigField 'String', 'YELP_CONSUMER_KEY', YelpConsumerKey 
     it.buildConfigField 'String', 'YELP_CONSUMER_SECRET', YelpConsumerSecret 
     it.buildConfigField 'String', 'YELP_TOKEN', YelpToken 
     it.buildConfigField 'String', 'YELP_TOKEN_SECRET', YelpTokenSecret 
    } 

} 

repositories { 
    mavenCentral() 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    apt 'net.simonvt.schematic:schematic-compiler:0.6.3' 

    compile 'net.simonvt.schematic:schematic:0.6.3' 
    compile 'com.android.support:appcompat-v7:24.1.1' 
    compile 'com.google.firebase:firebase-ads:9.0.2' 
    //compile 'com.google.android.gms:play-services-maps:9.2.1' 
    compile 'com.google.android.gms:play-services-maps:9.0.2' 
    compile 'com.google.android.gms:play-services-location:9.0.2' 
    compile 'com.yelp.clientlib:yelp-android:2.0.0' 
    compile 'org.scribe:scribe:1.3.7' 
    //compile 'com.google.android.gms:play-services:9.2.1' 
} 

Manifest

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

    <!-- 
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but you must specify either coarse or fine 
     location permissions for the 'MyLocation' functionality. 
    --> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/> 
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/> 
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/Theme.AppCompat"> 
     <uses-library android:name="com.google.android.maps"/> 

     <!-- 
      The API key for Google Maps-based APIs is defined as a string resource. 
      (See the file "res/values/google_maps_api.xml"). 
      Note that the API key is linked to the encryption key used to sign the APK. 
      You need a different API key for each encryption key, including the release key that is used to 
      sign the APK for publishing. 
      You can define the keys for the debug and release targets in src/debug/ and src/release/. 
     --> 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key"/> 

     <activity 
      android:name=".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> 

     <!--<activity--> 
      <!--android:name=".MapsActivity"--> 
      <!--android:label="@string/app_name">--> 
      <!--<meta-data--> 
       <!--android:name="android.support.PARENT_ACTIVITY"--> 
       <!--android:value="mem.edu.joshua.MainActivity"/>--> 

      <!--<intent-filter>--> 
       <!--<action android:name="android.intent.action.MAIN"/>--> 

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

     <!-- android:accountType="com.clashtoolkit.clashtoolkit" --> 
     <service android:name=".sync.AuthenticatorService"> 
      <intent-filter> 
       <action android:name="android.accounts.AccountAuthenticator"/> 
      </intent-filter> 

      <meta-data 
       android:name="android.accounts.AccountAuthenticator" 
       android:resource="@xml/authenticator"/> 
     </service> 

     <!-- The SyncAdapter service --> 
     <!-- android:allowParallelSyncs="true" --> 
     <service 
      android:name=".sync.SyncService" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="android.content.SyncAdapter"/> 
      </intent-filter> 

      <meta-data 
       android:name="android.content.SyncAdapter" 
       android:resource="@xml/syncadapter"/> 
     </service> 

     <provider 
      android:name=".data.generated.QuoteProvider" 
      android:authorities="mem.edu.joshua.data.QuoteProvider" 
      android:exported="false"/> 

    </application> 

</manifest> 

В mainActivity.java

@Override 
    public void onConnected(@Nullable Bundle bundle) { 

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(60000); 


     try { 

      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (LocationListener) this); 

      if (mLocationRequest != null) { 
       Log.e(LOG_TAG, "google working"); 



//    FragmentManager manager = getSupportFragmentManager(); 
//    FragmentTransaction transaction = manager.beginTransaction(); 
//    SupportMapFragment fragment = new SupportMapFragment(); 
//    transaction.add(R.id.mapView, fragment); 
//    transaction.commit(); 

       LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
       Criteria criteria = new Criteria(); 
       String provider = service.getBestProvider(criteria, false); 
       Location location = service.getLastKnownLocation(provider); 


       coord.setLon(location.getLongitude()); 
       coord.setLat(location.getLatitude()); 

       sPref.saveCoordBody("lat", coord.getLat()); 
       sPref.saveCoordBody("lon", coord.getLon()); 

        getSupportFragmentManager() 
          .beginTransaction() 
          .add(R.id.container, new MapsActivity(), MapsActivity.LOG_TAG) 
          .disallowAddToBackStack() 
          .commit(); 


      } 

     }catch (SecurityException e) { 
      Log.e(LOG_TAG, e.getMessage(), e); 
      e.printStackTrace(); 
     } 


    } 

В MapsActivity.java (фрагмент -> импорт android.support.v4.app.FragmentActivity;)

public class MapsActivity extends Fragment implements OnMapReadyCallback, LoaderManager.LoaderCallbacks<Cursor> { 

GoogleApiClient.ConnectionCallbacks, 
     public static final String LOG_TAG = MapsActivity.class.getSimpleName(); 

    private static final int CURSOR_LOADER_ID = 0; 

    private Context mContext; 


    private MapCursor mMapCursor; 
    protected Location mLastLocation; 
    private Cursor initQueryCursor; 
    private GetSet coord; 
    private AppPreferences sPref; 
    private LatLng home; 
    private GoogleMap mMap; 
    MapView mMapView; 

    private GoogleMap googleMap; 

     private static final String FRAGMENT_LISTS = 
       "net.simonvt.schematic.samples.ui.SampleActivity.LISTS"; 




    public MapsActivity() { 
    } 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     sPref = new AppPreferences(getActivity()); 

     SyncAdapter.syncImmediately(getActivity(), 
       sPref.getCoordBody("lat"), 
       sPref.getCoordBody("lon")); 

    } 

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

     //getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this); 


     View v = inflater.inflate(R.layout.yelp_google_map, container, 
       false); 


     mMapView = (MapView) v.findViewById(R.id.mapView); 

     mMapView.onCreate(savedInstanceState); 

     mMapView.onResume(); 

     mMapView.getMapAsync(this); 

     return v; 

    } 


    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
     return new CursorLoader(getActivity(), QuoteProvider.Quotes.CONTENT_URI, 
       new String[]{ QuoteColumns._ID, QuoteColumns.DISPLAY_ADDRESS, QuoteColumns.DISPLAY_PHONE, 
         QuoteColumns.RATING, QuoteColumns.URL, QuoteColumns.POSTAL_CODE, QuoteColumns.ID_BUSINESS_NAME, 
         QuoteColumns.LATITUDE, 
         QuoteColumns.LOGITUDE}, 
       null, 
       null, 
       null); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 

     if (loader != null) { 
      //  mMapCursor.swapCursor(data); 
      initQueryCursor = data; 
     } 
    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> loader) { 

    } 


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


    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

     mMap = googleMap; 

     initQueryCursor = getContext().getContentResolver().query(QuoteProvider.Quotes.CONTENT_URI, 
       new String[]{QuoteColumns.LATITUDE, QuoteColumns.LOGITUDE, QuoteColumns.ID_BUSINESS_NAME}, null, 
       null, null); 


     if (initQueryCursor != null) { 

      DatabaseUtils.dumpCursor(initQueryCursor); 
      initQueryCursor.moveToFirst(); 
      for (int i = 0; i < initQueryCursor.getCount(); i++) { 

       home = new LatLng(Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("latitude"))), 
         Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("longitude")))); 
       mMap.addMarker(new MarkerOptions().position(home). 
         title(initQueryCursor.getString(initQueryCursor.getColumnIndex("id_bussines_name")))); 

       initQueryCursor.moveToNext(); 

       builder.include(home); 
      } 
      if(home!=null) { 
       mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 91, 91, 42)); 
      } 
     } 
    } 
} 

Мой yelp_google_map.xml

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

    <!--<include--> 
     <!--android:id="@+id/map_toolbar"--> 
     <!--layout="@layout/app_bar" />--> 



    <LinearLayout 

     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <com.google.android.gms.maps.MapView 
      android:id="@+id/mapView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.MapFragment" /> 
     <!--was SupportMapFragment--> 
    </LinearLayout> 
</LinearLayout> 

ответ

0

После отчаянные попытки, я нашел решение, данное в аналогичной теме, Mrigank here Я верю после того, как изменил код, даже если он раздувается. nt контейнер как-то недействителен.

So My yelp_google_map.xml выглядит следующим образом.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context="mem.edu.joshua.MapsActivity" 
       android:background="#009688"> 


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

</RelativeLayout> 

Я избавилась от @Override общественности недействительного onDestroyView части

onCreateView остается, как этот

верхних переменные:
частного SupportMapFragment SMAP;
частный FragmentManager fm;
приватная GoogleMap mMap;

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

     getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this); 
     View v = inflater.inflate(R.layout.yelp_google_map, container, false); 

     fm=getChildFragmentManager(); 

     sMap = ((SupportMapFragment) fm.findFragmentById(R.id.map)); 

     mMap = sMap.getMap(); 


     initQueryCursor = getActivity().getContentResolver().query(QuoteProvider.Quotes.CONTENT_URI, 
       new String[]{QuoteColumns.LATITUDE, QuoteColumns.LOGITUDE, QuoteColumns.ID_BUSINESS_NAME}, null, 
       null, null); 

     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
     if (initQueryCursor != null) { 

      DatabaseUtils.dumpCursor(initQueryCursor); 
      initQueryCursor.moveToFirst(); 
      for (int i = 0; i < initQueryCursor.getCount(); i++) { 

       home = new LatLng(Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("latitude"))), 
         Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("longitude")))); 
       mMap.addMarker(new MarkerOptions().position(home). 
         title(initQueryCursor.getString(initQueryCursor.getColumnIndex("id_bussines_name")))); 

       initQueryCursor.moveToNext(); 

       builder.include(home); 
      } 
      if(home!=null) { 
       mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 91, 91, 42)); 
      } 
     } 

     return v; 

    } 

Добавлено все это в том же фрагменте Java файл

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

     @Override 
     public void onPause() { 
      super.onPause(); 
      sMap.onPause(); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      sMap.onDestroy(); 
     } 

     @Override 
     public void onLowMemory() { 
      super.onLowMemory(); 
      sMap.onLowMemory(); 
     } 

Наконец в mainActivity.java

  getSupportFragmentManager() 
        .beginTransaction() 
        .add(R.id.map, new MapsActivity(), MapsActivity.LOG_TAG) 
        .disallowAddToBackStack() 
        .commitAllowingStateLoss(); 

Надеется, что это помогает кому-то. Для меня это был кошмар. Теперь мое приложение работает отлично.