Android Fragment Force关闭错误?

时间:2013-01-21 04:04:23

标签: android

我正在研究android片段,当我在Android 3.0版本中运行我的应用程序时,它工作正常,但当我试图在3.0以下运行时,它给出forceclose错误,我不明白错误是什么。以下是日志cat错误和我的代码。

如果在智能手机中运行我的应用程序,那么我需要在另一个窗口中显示视图而不打开新活动。你能指导我怎么做吗?它在平板电脑上运行良好。我正在为平板电脑发送工作屏幕截图。

01-21 09:29:24.272: E/AndroidRuntime(398): FATAL EXCEPTION: main
01-21 09:29:24.272: E/AndroidRuntime(398): java.lang.NoSuchMethodError: com.example.fragment_demo.MainActivity.getFragmentManager
01-21 09:29:24.272: E/AndroidRuntime(398):  at com.example.fragment_demo.MainActivity.onCreate(MainActivity.java:19)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.os.Looper.loop(Looper.java:123)
01-21 09:29:24.272: E/AndroidRuntime(398):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-21 09:29:24.272: E/AndroidRuntime(398):  at java.lang.reflect.Method.invokeNative(Native Method)
01-21 09:29:24.272: E/AndroidRuntime(398):  at java.lang.reflect.Method.invoke(Method.java:507)
01-21 09:29:24.272: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-21 09:29:24.272: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-21 09:29:24.272: E/AndroidRuntime(398):  at dalvik.system.NativeStart.main(Native Method)



public class MainActivity extends Activity { 
    Button b1, b2, b3;   
    @Override   
    public void onCreate(Bundle savedInstanceState)
    {       
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main_fdemo);     
        FragmentManager fm = getFragmentManager();   
        FragmentTransaction ft = fm.beginTransaction();  
        StartFragment sf = new StartFragment();   
        ft.add(R.id.myFragment, sf);     
        ft.commit();      
        b1 = (Button) findViewById(R.id.button1);     
        b2 = (Button) findViewById(R.id.button2);   
        b3 = (Button) findViewById(R.id.button3); 

        b1.setOnClickListener(onButtonClick);  
        b2.setOnClickListener(onButtonClick);  
        b3.setOnClickListener(onButtonClick); 
    }   
    Button.OnClickListener onButtonClick = new Button.OnClickListener(){ 
        @Override    
        public void onClick(View v) 
        {        
            // TODO Auto-generated method stub     
            Fragment fragment = null;     
            if(v == b1){    
                fragment = new Fragment1();   
            }else if(v == b2){     
                fragment = new Fragment2();    
            }else if(v == b3){       
                fragment = new Fragment3();   
            }        
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.myFragment, fragment);    
            ft.commit();  
        }   
    };

}

main_fdemo.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment 3" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/myFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="3" >
    </LinearLayout>

</LinearLayout>


public class StartFragment extends Fragment
{     
    @Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {      
        // TODO Auto-generated method stub      
        return inflater.inflate(R.layout.start_fragment, container, false); 
    }
}

start_fragment.xml:-

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Select the Fragments"
        android:textSize="20dp" />

</LinearLayout>

public class Fragment1 extends Fragment
{     
    @Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {      
        // TODO Auto-generated method stub      
        return inflater.inflate(R.layout.fragment_1, container, false); 
    }
}

fragment_1.xml code:-

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Select the Fragments"
        android:textSize="20dp" />

</LinearLayout>

enter image description here

3 个答案:

答案 0 :(得分:1)

嗨请参考android文档。 Android Fragment仅在3.0及更高版本中受支持。所以3.0以下的片段和片段管理器将无法使用。如果你想使用片段和片段管理器使用android兼容库,请参阅此链接 Support Library

答案 1 :(得分:0)

您需要使用Android支持库向后兼容API 4.使用库时,您将使用支持方法,如getSupportFragmentManager()而不是getFragmentManager()。 http://developer.android.com/training/basics/fragments/support-lib.html

答案 2 :(得分:0)

您可以使用Android支持库在3.0以下设备中添加片段支持 检查此链接 http://developer.android.com/training/basics/fragments/support-lib.html