2014-10-03 24 views
-1

試圖獲得Fragment使用android.support.v4.appGingerbread加載薑餅工作,但我不斷收到以下錯誤:Android的 - 越來越片段使用android.support.v4.app

The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Welcome)

我歡迎類擴展片段,不知道我在這裏做錯了什麼?

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     loadfrag(); 
    } 

    private void loadfrag() { 
     Welcome frag = new Welcome(); 
     android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     transaction.replace(R.id.flactivity_main, frag); **<--- line with error** 
     transaction.commit(); 
    } 
} 


import android.support.v4.app.Fragment; 

public class Welcome extends Fragment{ 
    private final String TAG = this.getClass().getSimpleName(); 
    private ViewGroup mrootview; 
    private Activity mactivity; 
    private Context mcontext; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     //mrootview = (ViewGroup) 
     return inflater.inflate(R.layout.welcome, container, false); 

    } 
} 
+0

可能的重複:http://stackoverflow.com/questions/11619573/fragment-add-or-replace-not-working – Abdellah 2014-10-03 21:02:06

回答

3

而不是getFragmentManager()方法使用getSupportFragmentManager()