2013-03-29 32 views
0

谷歌地圖應用程序,我試着打的GetMap()在我的應用程序,並總是在行的GetMap()在Android中

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

用於顯示GoogleMap的活動是貫徹作爲片段出現錯誤。什麼可能是錯的? 我的整個活動如下所示。

package sg.SanThit.TrackMe; 

import android.annotation.SuppressLint; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 

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

public class MyDetailsFragment extends Fragment { 
private TrackerInfo trackerDetailInfo; 
GoogleMap map; 



public MyDetailsFragment() {  
} 

@SuppressLint("NewApi") 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); 
     map = ((SupportMapFragment)  
     getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     if (map == null) { 
      Toast.makeText(getActivity(), "Google Maps not available", 
    Toast.LENGTH_LONG).show(); 
     } 
}} 
+0

什麼是錯誤? –

回答

0

使用FragmentActivity而不是public class MyDetailsFragment extends Fragment

下面是加載圖示例代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 

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

這裏R.layout.activity_map是包含在地圖片段的XML文件。

+0

當我更改爲FragmentActivity時,我在從主要活動到詳細活動的片段事務中遇到問題。在片段fragment = new MyDetailsFragment(); – Bryanyan

+0

由於現在您的'DetailsFragment'類擴展了'FragmentActivity',將其更改爲'FragmentActivity fragment = new MyDetailsFragment();'。 –

+0

@Bryanyan:這是否解決了錯誤? –

相關問題