2012-03-19 59 views
0

它導致得到片段中的地圖視圖我導航到另一個片段並返回到地圖視圖時出現錯誤。我已經創建了一個對象,以取代片段,但我得到登錄貓片段已經添加碎片在地圖應用程序中的Backstack問題

package com.Raja.Avantaj; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 

import android.app.Fragment; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MapFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View v = inflater.inflate(R.layout.mapfragment, container, false); 
     return v; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 
     MapView map = new MapView(getActivity(), "0LYqpa9x_HkkUzATV8Rt5MHwhUuvm7sbnxsvZWQ"); 
     map.setClickable(true); 
     map.getController().setCenter(new GeoPoint((int)(8.532138 * 1E6),(int)(76.961583 * 1E6))); 
     map.getController().setZoom(9); 
     map.setBuiltInZoomControls(true); 
     Drawable marker=getResources().getDrawable(R.drawable.balloon); 
     marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
       marker.getIntrinsicHeight()); 
     map.getOverlays().add(new AvantajOverLay(marker)); 
     MyLocationOverlay me=new MyLocationOverlay(getActivity(), map); 
     map.getOverlays().add(me); 
     ((ViewGroup)getView()).addView(map); 
    } 

} 

我打電話片段這裏

package com.Raja.Avantaj; 

import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 

/** 
* @author user5 
* 
*/ 
public class ContactUs extends Fragment { 
    Button mapus; 
    View v; 
    Fragment frag1; 
    /* (non-Javadoc) 
    * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) 
    */ 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     v = inflater.inflate(R.layout.contactus, container, false); 
     intilize(); 
     return v; 
    } 
    // add a button to display the map 
    private void intilize() { 
     // TODO Auto-generated method stub 
     mapus = (Button)v.findViewById(R.id.map); 
     mapus.setOnClickListener(btnOnClickListener); 

    } 

    Button.OnClickListener btnOnClickListener = new Button.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      /*Toast.makeText(getActivity(),"Why didn't come", Toast.LENGTH_LONG).show(); 
      Intent intent=new Intent(v.getContext(),MapUs.class); 
      ContactUs.this.startActivity(intent);*/ 

      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 



      if(getFragmentManager().findFragmentByTag("MapFragment")==null) 
      { 
       Log.d("in null","in null"); 
       frag1 = new MapFragment(); 
       fragmentTransaction.replace(R.id.layoutfordetails, frag1,"MapFragment");  
       //fragmentTransaction.addToBackStack("MapFragment"); 
      } 


       else 
       { 
        Log.d("in not null","in not null"); 
        Fragment f11 = getFragmentManager().findFragmentByTag("MapFragment"); 
        fragmentTransaction.replace(R.id.layoutfordetails, f11,"MapFragment"); 

        //fragmentTransaction.addToBackStack("MapFragment"); 
       } 
      fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
      fragmentTransaction.commit(); 


     } 

    }; 

} 

的錯誤,我得到這個logcat的

03-19 13:16:45.214: W/dalvikvm(425): threadid=1: thread exiting with uncaught exception (group=0x40014760) 
03-19 13:16:45.294: E/AndroidRuntime(425): FATAL EXCEPTION: main 
03-19 13:16:45.294: E/AndroidRuntime(425): java.lang.IllegalStateException: Fragment already added: MapFragment{41000090 #2 id=0x7f080018 MapFragment} 
03-19 13:16:45.294: E/AndroidRuntime(425): at android.app.BackStackRecord.doAddOp(BackStackRecord.java:322) 
03-19 13:16:45.294: E/AndroidRuntime(425): at android.app.BackStackRecord.replace(BackStackRecord.java:360) 
03-19 13:16:45.294: E/AndroidRuntime(425): at com.Raja.Avantaj.ContactUs$1.onClick(ContactUs.java:70) 
03-19 13:16:45.294: E/AndroidRuntime(425): at android.view.View.performClick(View.java:3110) 

回答

0

創建MapFragment的新實例,即使它不爲null也是如此。

+0

我在其他情況下創建了一個實例,我再次得到相同的錯誤。 – 2012-03-19 09:29:10

+0

嘗試刪除以前的實例 – 2012-03-19 10:01:17