2017-06-01 43 views
-1

我剛剛開始用android片段和git卡住,同時從internet中實現一個示例。在下面附加以下代碼。它將非常感謝您獲得一些幫助。 我在行ft.add(R.id.fragmentone,fragment)發生錯誤。 此致敬禮。片段事務中的錯誤

MainActivity.java

import android.app.Fragment; 
    import android.app.FragmentManager; 
    import android.app.FragmentTransaction; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    public class MainActivity extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     } 
     public void getfragment(View view) { 
      FragmentOne fragment = new FragmentOne(); 
      FragmentManager fm=getFragmentManager(); 
      FragmentTransaction ft=fm.beginTransaction(); 
      ft.add(R.id.fragmentone,fragment); 
     } 
    } 

FragmentOne.java

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
public class FragmentOne extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_fragment_one, container,false); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.vighnesh.fragmentsexp.MainActivity" 
    android:orientation="vertical"> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/buttonfragment" 
    android:layout_margin="20dp" 
    android:text="Click to open fragment"/> 
    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/fragmentone" 
     android:layout_margin="20dp"> 
    </fragment> 
</LinearLayout> 

fragment_fragment_one.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.vighnesh.fragmentsexp.FragmentOne"> 
    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 
</FrameLayout> 
+0

什麼問題??? –

+0

我想你忘了從你的activity的onCreate方法調用getfragment()方法,你也需要通過ft.commit()提交你的fragment事務。 –

+0

不是一個好方法'返回inflater.inflate(R.layout.' –

回答

0

將此代碼替換爲您的活動。

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

} 

public void getfragment() { 
    FragmentOne fragment = new FragmentOne(); 
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.add(R.id.fragmentone, fragment); 
    ft.commit(); 
} 
0

改變它。

import android.app.Fragment; 
    import android.app.FragmentManager; 
    import android.app.FragmentTransaction; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    public class MainActivity extends AppCompatActivity { 

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

     Button btnFragOne = (Button)findViewById(R.id.buttonfragment); 
     btnFragOne.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       FragmentOne fragment = new FragmentOne(); 
       FragmentManager fm=getFragmentManager(); 
       FragmentTransaction ft=fm.beginTransaction(); 
       ft.add(R.id.fragmentone,fragment); getfragment(); 
      } 
     }); 

    } 
    } 
0

因此,使用getSupportFragmentManager()代替getFragmentManager()

因爲FragmentOne是從支持庫(android.support.v4.app.Fragment)不是從框架(android.app.Fragment),你需要使用android.support.v4.app.FragmentManagerFragmentOne做交易。