-1

hi im try to TabFragment to FragmentActivity. Как я могу вернуть активность карт Google. TabFragment.javaКак вернуть TabFragment в FragmentActivity

public class TabFragment extends Fragment { 

public static TabLayout tabLayout; 
public static ViewPager viewPager; 
public static int int_items = 2; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    /** 
    *Inflate tab_layout and setup Views. 
    */ 
    View x = inflater.inflate(R.layout.tab_layout, null); 
    tabLayout = (TabLayout) x.findViewById(R.id.tabs); 
    viewPager = (ViewPager) x.findViewById(R.id.viewpager); 

    /** 
    *Set an Apater for the View Pager 
    */ 
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 

    /** 
    * Now , this is a workaround , 
    * The setupWithViewPager dose't works without the runnable . 
    * Maybe a Support Library Bug . 
    */ 

    tabLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      tabLayout.setupWithViewPager(viewPager); 
     } 
    }); 

    return x; 

} 

class MyAdapter extends FragmentPagerAdapter { 

    public MyAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    /** 
    * Return fragment with respect to Position . 
    */ 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return new PrimaryFragment(); 
      case 1: 
       return new SocialFragment(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 

     return int_items; 

    } 

    /** 
    * This method returns the title of the tab according to the position. 
    */ 

    @Override 
    public CharSequence getPageTitle(int position) { 

     switch (position) { 
      case 0: 
       return "Primary"; 
      case 1: 
       return "Social"; 

     } 
     return null; 
    } 
} 

SocialFragment.java

public class SocialFragment extends Fragment { 


private TextView textView; 

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

    return inflater.inflate(R.layout.activity_maps, null); 
}} 

обычно является правильным. но я пытаюсь показать google-карты в SocialFragment.xml. карты показывают, но функции не работают (например, маркер)

Как я могу изменить эту строку следующим образом.

@Override 
public Fragment getItem(int position) { 
    switch (position) { 
     case 0: 
      return new PrimaryFragment(); 
     case 1: 
      return new MapsActivity(); 
    } 
    return null; 
} 

при попытке этот код ошибки вроде этого как можно исправить эту проблему. enter image description here

Это xml-файлы.

activity_maps.xml

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


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

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 
private static final LatLng KibleKordinat = new LatLng(21.42247, 39.82619); 
GoogleMap googleMap; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // 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(27.700769, 85.300140); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Kathmandu, Nepal")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 

}} 

ответ

0

Просто преобразовать код mapsactivity в некоторой MapFragment и you'r установить

+0

, что вы Вы можете объяснить? – ferfeit

+0

См. Здесь У вас есть активность @Override public Fragment getItem (int position) { переключатель (позиция) { case 0: return new PrimaryFragment(); кейс 1: возвращение новых картActivity(); } null null; } Теперь добавьте все свои основные функциональные возможности MapsActivity в фрагмент некоторых карт, а затем верните этот фрагмент вместо этого нового MapsActivity() –

Смежные вопросы