2017-07-22 72 views
0

我在StackOverflow上檢查了幾個解決方案,但沒有解決我的特殊問題。
我有幾個片段,我想要一個谷歌地圖活動。
所以我準備了佈局,並用必要的名稱創建了一個新的Google地圖活動。Map in Fragment:不兼容的類型無法轉換爲片段

當我嘗試運行它,它說:

Error:(20, 42) error: incompatible types: FragmentScreenD cannot be converted to Fragment 

而且它是寫

"Wrong 2nd argument type. Found "com.example.name.yapplication.FragmentScreenD", required "android.support.v4.app.Fragment". 

這樣的消息並沒有消失之後。

package com.example.name.myapplication; 

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 


public class MainActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle arg0) { 
     super.onCreate(arg0); 
     setContentView(R.layout.activity_main); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.fragment1, new FragmentScreenA()).commit(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.fragment2, new FragmentScreenB()).commit(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.fragment3, new FragmentScreenC()).commit(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.fragment4, new FragmentScreenD()).commit(); 
    } 
} 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:orientation="horizontal" 
    android:paddingBottom="10dp" 
    android:weightSum="2" > 

    <FrameLayout 
     android:id="@+id/fragment3" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="0dp" 
     android:layout_weight="1" /> 

    <FrameLayout 
     android:id="@+id/fragment4" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

package com.example.name.myapplication; 

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

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 FragmentScreenD extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_fragment_screen_d); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 
+0

你進口一切從android.SUPPORT包? –

回答

0

FragmentScreenD其延伸從FragmentActivity不片段和FragmentManager的替換方法的第二arguement需要的片段,FragmentScreenD可以經由意圖被啓動,而且其行爲是像一個活動,將FragmentActivity更改爲Fragment並實施FragmentScreenA,B,C中的方法...

0

我有幾個片段,我想在其中一個谷歌地圖活動。

你不想要的活動,你想有一個片段,作爲錯誤是想告訴你。

您可以直接添加new SupportMapFragment()到FrameLayout裏,或者你需要自行擴展,與其片段活動

相關問題