0

当我在 android 3.0 及更高版本中运行我的应用程序时,我正在处理 android 片段,它工作正常,但是当我尝试在 3.0 以下运行时,它给出了 forceclose 错误,我不明白出了什么问题。下面是日志猫错误和我的代码。

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

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>

在此处输入图像描述

4

3 回答 3

1

请参考安卓文档。AndroidFragment仅支持 3.0 及更高版本。所以低于 3.0 的片段和片段管理器将不可用。如果您想使用片段和片段管理器,请使用 Android 兼容库并参考支持库

于 2013-01-21T04:09:10.247 回答
0

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

于 2013-01-21T04:08:09.543 回答
0

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

于 2013-01-21T04:10:25.593 回答