将片段添加到抽象活动

时间:2018-06-21 09:47:47

标签: android android-fragments fragmenttransaction

我正在尝试向抽象活动添加片段。这是一个基本活动,我的应用程序中的所有其他活动都从此扩展。

我的问题:将片段添加到基本活动中时,出现以下错误。

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f09011d (com.musicplayer.musicana:id/nowPlayingScreensContainer) for fragment SlidingUpPanelFragment{961e486 #0 id=0x7f09011d}

我很确定我在添加片段时正在使用基本活动中存在的framelayout的ID。

baseActivity的XML:

<com.sothree.slidinguppanel.SlidingUpPanelLayout

    xmlns:sothree="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoPanelHeight="70dp"
    sothree:umanoShadowHeight="10dp"
    android:background="#000"
    sothree:umanoFadeColor="#b2000000"
    >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />


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




</com.sothree.slidinguppanel.SlidingUpPanelLayout>

其中有两个框架布局,第一个用于添加活动,第二个用于添加片段。

添加片段的代码(用onCreate编写):

 SlidingUpPanelFragment fragment = SlidingUpPanelFragment.newInstance();
         FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.nowPlayingScreensContainer, fragment);
    fragmentTransaction.commit();

完整日志:

Failed to capture application
06-21 14:54:22.002 2601-2601/com.musicplayer.musicana E/FragmentManager: No view found for id 0x7f09011d (com.musicplayer.musicana:id/nowPlayingScreensContainer) for fragment SlidingUpPanelFragment{961e486 #0 id=0x7f09011d}
    Activity state:
06-21 14:54:22.014 2601-2601/com.musicplayer.musicana E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.musicplayer.musicana, PID: 2601
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.musicplayer.musicana/com.musicplayer.musicana.splash}: java.lang.IllegalArgumentException: No view found for id 0x7f09011d (com.musicplayer.musicana:id/nowPlayingScreensContainer) for fragment SlidingUpPanelFragment{961e486 #0 id=0x7f09011d}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2699)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6236)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
     Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f09011d (com.musicplayer.musicana:id/nowPlayingScreensContainer) for fragment SlidingUpPanelFragment{961e486 #0 id=0x7f09011d}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1422)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
        at android.app.Activity.performStart(Activity.java:6701)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2662)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6236) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 

基本活动的OnCreate:

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



        slidingUpPanelLayout = findViewById(R.id.sliding_layout);
        slidingUpPanelLayout.addPanelSlideListener(this);
        storageUtil = new StorageUtil(getApplicationContext());

        pref = getSharedPreferences(TAG,MODE_PRIVATE);
        registerSelectScreenReceiver();

         SlidingUpPanelFragment fragment = SlidingUpPanelFragment.newInstance();
         FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.nowPlayingScreensContainer, fragment);
    fragmentTransaction.commit();


        }

很乐意提供更多代码。该如何解决?

0 个答案:

没有答案