从主类调用FragmentActivity(appCompatActivity)

时间:2016-02-01 15:08:22

标签: java android android-fragments android-fragmentactivity

我有主类和更多2个片段类,但其中一个是从FragmentActivity扩展的,当我从一个没有从FragmentActivity扩展但只是从Fragment扩展的类调用一个片段时,它可以工作。我如何调用从appCompacAtivity扩展的主类中的FragmentActivity扩展的片段?

以下是我的主要活动代码( Principal_eng.Java ):

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

public class Frag1 extends FragmentActivity {
    public View onCreateView(LayoutInflater inf, ViewGroup cont, Bundle savedInst) {
        View v = inf.inflate(R.layout.frag1, cont, false);
        return v;
    }
}

以下是片段1( Frag1.Java

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

public class Frag2 extends Fragment{
    public View onCreateView(LayoutInflater inf,ViewGroup cont, Bundle savedInst){
        View v = inf.inflate(R.layout.frag2,cont,false);
        return v;
    }
}

片段2中的代码( Frag2.Java

--------- beginning of main
I/installd(    0): installd firing up
I/Netd    (    0): Netd 1.0 starting
E/Netd    (    0): Failed to open /proc/sys/net/ipv6/conf/default/accept_ra_rt_table: No such file or directory
E/Netd    (    0): Failed to open /proc/sys/net/ipv6/conf/eth0/accept_ra_rt_table: No such file or directory
E/Netd    (    0): Failed to open /proc/sys/net/ipv6/conf/lo/accept_ra_rt_table: No such file or directory
E/Netd    (    0): Failed to open /proc/sys/net/ipv6/conf/sit0/accept_ra_rt_table: No such file or directory
I/        (    0): debuggerd: Feb  1 2016 18:35:07
--------- beginning of system
I/Vold    (   84): Vold 2.1 (the revenge) firing up
I/lowmemorykiller(   82): Using in-kernel low memory killer interface
I/keystore(   94): SELinux: Keystore SELinux is disabled.
I/SurfaceFlinger(   85): SurfaceFlinger is starting
I/SurfaceFlinger(   85): SurfaceFlinger's main thread ready to run. Initializing graphics H/W...
F/libEGL  (   85): couldn't find an OpenGL ES implementation
--------- beginning of crash
F/libc    (   85): Fatal signal 6 (SIGABRT), code -6 in tid 85 (surfaceflinger)
I/DEBUG   (   89): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (   89): Build fingerprint: 'unknown'
I/DEBUG   (   89): Revision: '10489921'
I/DEBUG   (   89): ABI: 'arm'
I/DEBUG   (   89): pid: 85, tid: 85, name: surfaceflinger  >>> /system/bin/surfaceflinger <<<
I/DEBUG   (   89): signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
I/DEBUG   (   89): Abort message: 'couldn't find an OpenGL ES implementation'
I/DEBUG   (   89):     r0 00000000  r1 00000055  r2 00000006  r3 00000000
I/DEBUG   (   89):     r4 b6f3ce38  r5 00000006  r6 00000002  r7 0000010c
I/DEBUG   (   89):     r8 b6082000  r9 00000000  sl 00000000  fp be942b0c
I/DEBUG   (   89):     ip 00000055  sp be942580  lr b6e99745  pc b6ebcbd8  cpsr 60000010
I/DEBUG   (   89): 
I/DEBUG   (   89): backtrace:
I/DEBUG   (   89):     #00 pc 00039bd8  /system/lib/libc.so (tgkill+12)
I/DEBUG   (   89):     #01 pc 00016741  /system/lib/libc.so (pthread_kill+52)
I/DEBUG   (   89):     #02 pc 0001735f  /system/lib/libc.so (raise+10)
I/DEBUG   (   89):     #03 pc 00013b39  /system/lib/libc.so (__libc_android_abort+36)
I/DEBUG   (   89):     #04 pc 00012f18  /system/lib/libc.so (abort+4)
I/DEBUG   (   89):     #05 pc 00007c51  /system/lib/libcutils.so (__android_log_assert+88)
I/DEBUG   (   89):     #06 pc 000431bd  /system/lib/libEGL.so
I/DEBUG   (   89):     #07 pc 00010795  /system/lib/libEGL.so
I/DEBUG   (   89):     #08 pc 00010f45  /system/lib/libEGL.so (eglGetDisplay+24)
I/DEBUG   (   89):     #09 pc 00017815  /system/lib/libsurfaceflinger.so (android::SurfaceFlinger::init()+60)
I/DEBUG   (   89):     #10 pc 00000af1  /system/bin/surfaceflinger
I/DEBUG   (   89):     #11 pc 00012dc9  /system/lib/libc.so (__libc_init+44)
I/DEBUG   (   89):     #12 pc 00000c08  /system/bin/surfaceflinger
I/DEBUG   (   89): 
I/DEBUG   (   89): Tombstone written to: /data/tombstones/tombstone_04
I/Netd    (  179): Netd 1.0 starting
E/Netd    (  179): Failed to open /proc/sys/net/ipv6/conf/default/accept_ra_rt_table: No such file or directory
E/Netd    (  179): Failed to open /proc/sys/net/ipv6/conf/eth0/accept_ra_rt_table: No such file or directory
E/Netd    (  179): Failed to open /proc/sys/net/ipv6/conf/lo/accept_ra_rt_table: No such file or directory
E/Netd    (  179): Failed to open /proc/sys/net/ipv6/conf/sit0/accept_ra_rt_table: No such file or directory
I/SurfaceFlinger(  180): SurfaceFlinger is starting
I/SurfaceFlinger(  180): SurfaceFlinger's main thread ready to run. Initializing graphics H/W...    

3 个答案:

答案 0 :(得分:1)

Frag1延伸FragmentActivity,而不是Fragment;它被宣布为活动。

答案 1 :(得分:1)

FragmentActivityActivity,而不是Fragment,因此您尝试做的事情没有意义:尝试在Activity上执行片段交易

为什么不像Fragment那样只是Frag2的子类?

答案 2 :(得分:0)

您的问题是您的Frag2类是一个活动而不是片段。你应该让它扩展Fragment而不是活动。您不能将活动放在片段容器中。

如果您想从mainActivity类调用您的活动,请执行以下活动:

Intent intent = new Intent(this, Frag1.class);
startActivity(intent);
祝你好运!