2013-02-15 86 views
1

我決定從多個活動切換到一個活動之間切換片段,但應用程序現在崩潰。Android崩潰在getSupportFragmentManager()

這是我加入片段

公共類MainActivity的活動來延長SherlockFragmentActivity {

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

    MyFragment fragment = new MyFragment(); 

    fragment.setArguments(getIntent().getExtras()); 

    getSupportFragmentManager().beginTransaction() 
    .add(R.id.fragment_container, fragment).commit(); 

} 

這裏是片段的觀察員,並有功能,但爲了節省空間生病就顯示出創作

public class MyFragment extends SherlockFragment implements Observer{ 

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

     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.my_fragment, container, false); 
    } 

繼承人my_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/my_fragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

... HAS SOME TEXT VIEWS HERE! 
</RelativeLayout> 

我得到的崩潰是

02-15 16:17:41.079: E/AndroidRuntime(18668): FATAL EXCEPTION: main 
02-15 16:17:41.079: E/AndroidRuntime(18668): java.lang.RuntimeException: 
Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: 
java.lang.IllegalArgumentException: 
No view found for id 0x7f040036 for fragment MyFragmentt{41a05910 #0 id=0x7f040036} 

誰能幫助我在這裏?我無法弄清楚是什麼導致了這一點。我知道如果我在主活動(這個帖子中的頂部代碼塊)中註釋掉getSupportFragmentManager(),它將運行而不會在我的片段中繪製任何東西。

UPDATE

這我不知道該frame_container在哪裏放置

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/fragment_container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" /> 
+0

你已經宣佈'FrameLayout'與ID'R.id.fragment_container'? – hardartcore 2013-02-15 16:36:13

+0

我有這個,但我不知道如果我有它在正確的地方應該是它自己的XML文件或應該在my_fragment.xml或我的activity_main.xml?第一次使用片段,所以我可以犯一個愚蠢的錯誤 user2015852 2013-02-15 16:42:28

+0

爲了便於閱讀,我添加框架容器到最初的問題 – user2015852 2013-02-15 16:46:28

回答

0

所以,你可以使用你的應用程序Fragments的方式有兩種。 第一種方式是,如果你在XML文件中聲明的片段是這樣的:

<fragment android:name="com.example.news.ArticleReaderFragment" 
     android:id="@+id/viewer" 
     android:layout_weight="2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 

第二種方式是,如果你添加/動態替換你的片段到你的容器,在最例子是FrameLayout。這裏是你如何能做到這一點:

在你的主FragmentActivity

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

,並在你的XML myFragmentContainer.xml是你把你的fragment_container,它看起來像:

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

和你添加並替換您的Fragments這樣:

if (findViewById(R.id.fragment_container) != null) { 

     if (savedInstanceState != null) { 
      return; 
     } 

     // Create an instance of ExampleFragment 
     HeadlinesFragment firstFragment = new HeadlinesFragment(); 
     // if there are any extras 
     firstFragment.setArguments(getIntent().getExtras()); 

     // Add the fragment to the 'fragment_container' FrameLayout 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, firstFragment).commit(); 
    } 

以及您想顯示只是做下一個片段:

getSupportFragmentManager().beginTransaction() 
        .replace(R.id.fragment_container, secondFragment).commit(); 
+0

感謝您的幫助,現在完美工作。 – user2015852 2013-02-15 18:12:01