我想创建一个具有我的缩放设置,标记和地图类型设置的自定义地图片段,以便可以在多个活动中重复使用它。我创建的类称为“ LiteMap”。

我从编译器收到错误
'Incompatible Types' 'Required: com.mypackage.LiteMap' 'Found com.google.android.gms.maps.SupportMapFragment'
我称之为.newInstance的地方。我假设.newInstance返回父类而不是我的类,但是我不确定如何在我的类中重写以返回我的“ LiteMap”对象。

作为一种替代方法,我尝试使用new实例化我的地图对象,但在没有任何特定行的情况下出现以下错误,但是使用new并没有创建LiteMap对象。我想做的事是不可能的还是愚蠢的?

03-29 20:36:52.771 11326-11326/com.tremulant.phield E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.tremulant.phield, PID: 11326
  java.lang.RuntimeException: Unable to resume activity {com.tremulant.phield/com.tremulant.phield.ReviewDetails}: java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ei.bz.o()' on a null object reference
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2974)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371)
      at android.app.ActivityThread.access$800(ActivityThread.java:149)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5290)
      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:908)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
   Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ei.bz.o()' on a null object reference
      at maps.ei.n.b(Unknown Source)
      at com.google.android.gms.maps.internal.i$a.onTransact(:com.google.android.gms.alldynamite:115)
      at android.os.Binder.transact(Binder.java:380)
      at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onResume(Unknown Source)
      at com.google.android.gms.maps.SupportMapFragment$zza.onResume(Unknown Source)
      at com.google.android.gms.dynamic.zza$7.zzb(Unknown Source)
      at com.google.android.gms.dynamic.zza.zza(Unknown Source)
      at com.google.android.gms.dynamic.zza.onResume(Unknown Source)
      at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
      at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
      at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2056)
      at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:196)
      at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:505)
      at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:494)
      at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:174)
      at android.app.Activity.performResume(Activity.java:6119)
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2963)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371) 
      at android.app.ActivityThread.access$800(ActivityThread.java:149) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5290) 
      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:908) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
03-29 20:36:52.822 11326-11326/? I/Process: Sending signal. PID: 11326 SIG: 9


LiteMap片段

package com.tremulant.phield;

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

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;

public class LiteMap extends SupportMapFragment implements OnMapReadyCallback {
    private GoogleMap mMap;

    String lat;
    String lng;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //super.onCreate(savedInstanceState);
        lat = getArguments().getString("Lat");
        lng = getArguments().getString("Lng");
        return inflater.inflate(R.layout.maplite,null);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        LatLng mapVenue = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
        mMap.addMarker(new MarkerOptions().position(mapVenue));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mapVenue, 14.0f));
    }
}


片段的maplite.xml布局文件

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="25dp"
          android:layout_height="fill_parent"
          android:weightSum="10">


<fragment
    android:id="@+id/lmap"
    android:layout_width="match_parent"
    android:layout_height="25dp"
    class="com.tremulant.phield.LiteMap" />

 </LinearLayout>



    package com.tremulant.phield;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ReviewDetails extends BaseActivity implements View.OnClickListener{

private Venue mVenue;
@Override
public void onClick(View v) {
    Intent mapIntent = new Intent(ReviewDetails.this, VenueMap.class);
    mapIntent.putExtra(VENUE_TRANSFER,mVenue);
    startActivity(mapIntent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_review_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    mVenue = (Venue) intent.getSerializableExtra(VENUE_TRANSFER);

    LiteMap liteMap = new LiteMap();
    //lmap.setArguments(args);
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.map_container, liteMap);
    fragmentTransaction.commit();
    }
}


content_review_details.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.tremulant.phield.ReviewDetails"
tools:showIn="@layout/activity_review_details">

<FrameLayout
    android:id="@+id/map_container"
    android:layout_below="@id/map"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    </FrameLayout>

 </RelativeLayout>


我简化了代码。另外我也不明白为什么我在这里粘贴代码时无法正确缩进。啊!现在,我的logcat循环显示了以下内容。当尝试加载ReviewDetails活动时,屏幕仅保持黑色。

03-30 20:58:28.364 17076-17076/com.tremulant.phield I/ViewRootImpl:

ViewRoot's Touch Event : ACTION_DOWN
03-30 20:58:28.466 17076-17076/com.tremulant.phield I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
03-30 20:58:28.503 17076-17076/com.tremulant.phield I/Timeline: Timeline: Activity_launch_request id:com.tremulant.phield time:25955914
03-30 20:58:28.552 17076-17076/com.tremulant.phield I/Timeline: Timeline: Activity_launch_request id:com.tremulant.phield time:25955964
03-30 20:58:28.600 17076-17076/com.tremulant.phield D/ContextHelper: convertTheme. context->name=com.tremulant.phield themeResourceId=2131427455
03-30 20:58:28.604 17076-17076/com.tremulant.phield D/ContextHelper: convertTheme. context->name=com.tremulant.phield themeResourceId=2131427455
03-30 20:58:28.606 17076-17076/com.tremulant.phield I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff00796b
03-30 20:58:28.753 17076-17076/com.tremulant.phield I/zzad: Making Creator dynamically
03-30 20:58:28.763 17076-17076/com.tremulant.phield W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
03-30 20:58:28.763 17076-17076/com.tremulant.phield W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
03-30 20:58:28.839 17076-17076/com.tremulant.phield D/ChimeraCfgMgr: Reading stored module config
03-30 20:58:28.878 17076-17076/com.tremulant.phield D/ChimeraFileApk: Primary ABI of requesting process is armeabi-v7a
03-30 20:58:28.879 17076-17076/com.tremulant.phield I/art: DexFile_isDexOptNeeded failed to open oat file '/data/dalvik-cache/arm/data@[email protected]@app_chimera@m@[email protected]@classes.dex' for file location '/data/data/com.google.android.gms/app_chimera/m/00000000/DynamiteModules-prod.apk': Failed to open oat filename for reading: No such file or directory
03-30 20:58:28.880 17076-17076/com.tremulant.phield D/ChimeraFileApk: Classloading successful. Optimized code found.
03-30 20:58:29.044 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services client version: 8487000
03-30 20:58:29.055 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services package version: 8703230
03-30 20:58:29.331 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services package version: 8703230
03-30 20:58:29.339 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services package version: 8703230
03-30 20:58:29.344 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services package version: 8703230
03-30 20:58:29.352 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services package version: 8703230
03-30 20:58:29.361 17076-17076/com.tremulant.phield I/Google Maps Android API: Google Play services package version: 8703230

最佳答案

如果您仍然需要答案:

将您的maplite.xml更改为:

<RelativeLayout
    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="25dp">

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

</RelativeLayout>


扩展android.support.v4.app.Fragment片段中的SupportMapFragment而不是LiteMap

onCreateView中的MapLite片段中放入:

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

    SupportMapFragment smf = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));

    smf.getMapAsync(this);
}

10-07 23:21