2014-06-13 141 views
1

我必須創建一個應用程序,我必須使用Fragment。 在MainActivity中,有一個webView。從SecondActivity I 開始使用片段。使用片段時面臨的問題

這裏是SecondActivity的代碼:

package com.dev.testapp; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.View; 

public class Second extends Activity { 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second); 


    } 

    public void choosefragment(View view) { 

     Fragment fg; 

     if(view == findViewById(R.id.secondbtn2)) { 

      fg = new SecondFragment(); 
     } 

     else 
     { 
      fg = new FirstFragment(); 
     } 

     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.frag1, fg); 
     fragmentTransaction.commit(); 
    } 

} 

Hhere是其second.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#ffffff" > 

    <Button 
    android:id="@+id/secondbtn1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="first fragment" 
    android:onClick="chooseFragment" /> 

    <Button 
    android:id="@+id/secondbtn2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="second fragment" 
    android:onClick="chooseFragment"/> 

    <fragment 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:name="com.dev.testapp.fragment1" 
     android:id="@+id/frag1" 
     /> 
</LinearLayout> 

之後,我創建了兩個班的片段,因爲它需要 碼FirstFragment:

package com.dev.testapp; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 

    public class FirstFragment extends Fragment{ 


     public View onCreate(LayoutInflater inflater, 
       ViewGroup container , Bundle savedInstanceState) { 

      //Inflate the layout for this fragment 

      return inflater.inflate(R.layout.fragment1,container, false); 

     } 

    } 

這裏是SecondFragment:

package com.dev.testapp; 

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

    public class SecondFragment extends Fragment{ 

     public View onCreate(LayoutInflater inflater, 
       ViewGroup container , Bundle savedInstanceState) { 

      //Inflate the layout for this fragment 

      return inflater.inflate(R.layout.fragment2,container, false); 

     } 

    } 

當我與這一切後,我的SecondActivity是顯示我不知道如何處理錯誤:

screen shot of error

回答

1

正如在以前的答案中指出的,您的問題是不正確的FirstFragment的片段庫。 FirstFragment.java的

更改線路5:

import android.support.v4.app.Fragment; 

到:

import android.app.Fragment; 
+0

哎肖恩,我要問你一個件事好友。解決這個問題後..我試圖運行到我的應用程序它給了我一個錯誤是:** java.lang.RuntimeException:無法實例化活動componentInfo(com.dev.testapp/com.dev.testapp.firstFragment ):java.lang.classCastException:com.dev.testapp.FirstFragment無法轉換爲android.app.activity ** – Devraj

+0

我不確定沒有更多信息,但它看起來像你可能試圖啓動一個片段作爲活動?你是否錯誤地將碎片包含在你的Manifest中? – Shaun

+0

其實我只使用Second.java作爲一個活動,並在manifest.xml文件中提到了這個問題。我是否需要從那裏刪除? – Devraj

1

您的活動中使用這import android.app.Fragment;

您的片段是import android.support.v4.app.Fragment;

所以,你要麼有兩種訊號分配延長在應用程序包或支持庫中查找片段

1

您的第二個片段的類型爲android.app.Fragment,而您的第一個片段爲android.support.v4.app.Fragment。