无法在所有活动中添加 SlidingUpPanel

Posted

技术标签:

【中文标题】无法在所有活动中添加 SlidingUpPanel【英文标题】:Can't add SlidingUpPanel across all activities 【发布时间】:2018-04-19 11:03:44 【问题描述】:

我正在尝试通过创建 baseActivity 在所有活动中从 umano (https://github.com/umano/androidSlidingUpPanel) 实施 SlidingUpPanel。但是在我创建了所有内容(baseactivity.java 和 baseactivity.xml)并将我的 MainActivity.java 扩展到 baseactivity 之后,它在 BaseActivity 中存在的每个视图上都给了我 nullpointerException。我似乎无法弄清楚为什么。 MainActivity 有自己独立的 .xml,我只想在不影响 MainActivity 现有布局的情况下添加 SlidingUpPanel。

我知道导致空指针异常的原因,但在这里我已经初始化了每个视图。帮帮我。

BaseActivity.java:

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


    prefs = getSharedPreferences(" com.musicplayer.musicana", MODE_PRIVATE);

    mContext = BaseActivity.this;
    shareSong = findViewById(R.id.shareSong);
    shareSong.setVisibility(View.INVISIBLE);
    nowPlaying = findViewById(R.id.nowPlaying);
    downArrow = findViewById(R.id.downArrow);
    downArrow.setVisibility(View.INVISIBLE);
    nowPlaying.setAlpha(0);
    backgroundImage = findViewById(R.id.background_image);
    shuffleSong = findViewById(R.id.shuffleSong);
    repeatSong = findViewById(R.id.repeatSong);
    nextSong= findViewById(R.id.nextImage);
    previousImage = findViewById(R.id.previousImage);
    newlr = findViewById(R.id.newlr);
    PlayPauseImage = findViewById(R.id.playpauseImage);
    durationTimer = findViewById(R.id.durationTimer);
    circleImage = findViewById(R.id.circularImage);
    circularSeekBar = findViewById(R.id.circularSeekBar);
    circularSeekBar.setEnabled(false);

    fullSongName = findViewById(R.id.fullSongName);
    fullArtistName = findViewById(R.id.fullArtistName);
    fullSongName.setAlpha(0);
    fullArtistName.setAlpha(0);
    linearLayoutSlide = findViewById(R.id.linearLayoutSlide);
    playSlideImage = findViewById(R.id.songButton);
    backarrow =  findViewById(R.id.imageView);
    SongNameSlide = findViewById(R.id.SongNameSlide);
    ArtistNameSlide = findViewById(R.id.ArtistNameSlide);

    storageUtil = new StorageUtil(getApplicationContext());

    slidingUpPanelLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);

    playSlideImage.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 

            if(playPause)
                ButtonPlay();
                startService(new Intent(BaseActivity.this, MediaPlayerService.class));
                Intent is3 = new Intent(ACTION_PLAY_SLIDE_PLAY_PAUSE);
                sendBroadcast(is3);
                playPause = false;
            

            else

                ButtonPause();
                startService(new Intent(BaseActivity.this, MediaPlayerService.class));
                Intent is3 = new Intent(ACTION_PLAY_SLIDE_PLAY_PAUSE);
                sendBroadcast(is3);

            



        
    );

    PlayPauseImage.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 


            Intent is2 = new Intent(ACTION_PLAY_PLAY_PAUSE);
            sendBroadcast(is2);


        
    );


    nextSong.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 


            Intent is = new Intent(ACTION_PLAY_NEXT);
            sendBroadcast(is);
        
    );

    previousImage.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 


            Intent is1 = new Intent(ACTION_PLAY_PREVIOUS);
            sendBroadcast(is1);
        
    );

    shuffleSong.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 


            Intent is4 = new Intent(ACTION_PLAY_SHUFFLE);
            sendBroadcast(is4);

        
    );

    repeatSong.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 


            Intent is5 = new Intent(ACTION_PLAY_REPEAT);
            sendBroadcast(is5);

        

    );

    shareSong.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 

            Intent is6 = new Intent(ACTION_PLAY_SHARE);
            sendBroadcast(is6);


        
    );



    downArrow.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);

        
    );




    backarrow.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
        
    );




    //



    //  slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN);

    slidingUpPanelLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() 
        @Override
        public void onPanelSlide(View panel, float slideOffset) 

            Log.i(TAG, "onPanelSlide, offset " + slideOffset);

            SongNameSlide.setAlpha(1 - slideOffset);
            ArtistNameSlide.setAlpha(1 - slideOffset);
            playSlideImage.setAlpha(1 - slideOffset);
            fullSongName.setAlpha(slideOffset);
            fullArtistName.setAlpha(slideOffset);
            nowPlaying.setAlpha(slideOffset);
            downArrow.setVisibility(View.VISIBLE);
            downArrow.setAlpha(slideOffset);
            shareSong.setVisibility(View.VISIBLE);
            shareSong.setAlpha(slideOffset);
            resideMenu.addIgnoredView(panel);


        


        @Override
        public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) 

            slidingUpPanelLayout.setDragView(panel);



            if (newState.equals(SlidingUpPanelLayout.PanelState.EXPANDED)) 

                playSlideImage.setEnabled(false);




             else if (newState.equals(SlidingUpPanelLayout.PanelState.COLLAPSED)) 

                playSlideImage.setEnabled(true);


            

        
    );




    

MainActivity.java:

 @Override
 protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    mContext = MainActivity.this;


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    
    sectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

    viewPager = (ParallaxViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    viewPager.setOffscreenPageLimit(6);



    smartTabLayout = (SmartTabLayout) findViewById(R.id.viewpagertab);
    smartTabLayout.setViewPager(viewPager);










    setUpMenu();




    

base_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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_
android:layout_
android:gravity="bottom"
sothree:umanoPanelHeight="70dp"
sothree:umanoShadowHeight="10dp"
android:background="#b9ffffff">

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_
    android:layout_>

</RelativeLayout>






<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayoutSlide"
    android:layout_
    android:layout_
    android:layout_marginTop="25dp"
    android:orientation="horizontal"
    tools:context="com.musicplayer.musicana"
    >

    <ImageView
        android:id="@+id/background_image"
        android:layout_
        android:layout_
        android:src="@drawable/party"
        android:scaleType="centerCrop"/>

    <LinearLayout
        android:id="@+id/newlr"
        android:layout_
        android:layout_
        android:background="#bd3b3838"
        >

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:paddingTop="15dp"
            >


            <TextView
                android:id="@+id/SongNameSlide"
                android:layout_
                android:layout_
                android:singleLine="true"
                android:text="Closer"
                android:textSize="15sp"
                android:textColor="#ffffff"
                android:paddingLeft="20dp"/>

            <TextView
                android:id="@+id/ArtistNameSlide"
                android:layout_
                android:layout_
                android:singleLine="true"
                android:text="Chainsmokers"
                android:textSize="10sp"
                android:textColor="#ffffff"
                android:paddingLeft="20dp"
                />

        </LinearLayout>

        <RelativeLayout
            android:layout_
            android:layout_>

            <ImageView
                android:id="@+id/songButton"
                android:layout_
                android:layout_
                android:src="@drawable/play_song"
                android:layout_marginTop="18dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="13dp"
                android:layout_marginEnd="13dp">

            </ImageView>


        </RelativeLayout>

    </LinearLayout>


    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/circularImage"
        android:layout_
        android:layout_
        android:layout_marginTop="85dp"
        android:layout_centerHorizontal="true"/>


    <me.tankery.lib.circularseekbar.CircularSeekBar
        android:id="@+id/circularSeekBar"
        android:layout_
        android:layout_
        android:layout_marginTop="60dp"
        android:layout_centerHorizontal="true"
        app:cs_circle_progress_color = "#ffffff"
        app:cs_pointer_stroke_width = "10dp"
        app:cs_pointer_halo_width = "10dp"
        app:cs_pointer_color = "#ffffff"
        app:cs_pointer_halo_color_ontouch = "#55ffffff"

        />





    <LinearLayout

        android:layout_
        android:layout_
        android:orientation="vertical"
        android:layout_marginTop="390dp"
        android:layout_centerHorizontal="true"
        >
        <TextView
            android:id="@+id/fullSongName"
            android:layout_
            android:layout_
            android:gravity="center"
            android:maxLines="1"
            android:textSize="16sp"
            android:textColor="#ffffff"
            android:text="SongName appears here"/>

        <TextView
            android:id="@+id/fullArtistName"
            android:layout_
            android:layout_
            android:layout_gravity="center"
            android:maxLines="1"
            android:textSize="10sp"
            android:textColor="#ffffff"
            android:paddingTop="10dp"
            android:text="\nArtistName appears here"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayoutsong"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/downArrow"
            android:layout_
            android:layout_
            android:paddingTop="7dp"
            android:paddingLeft="5dp"
            android:clickable="true"
            android:src="@drawable/down_arrow"/>

        <TextView
            android:id="@+id/nowPlaying"
            android:layout_
            android:layout_
            android:text="Now Playing"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:paddingLeft="20dp"
            android:paddingTop="10dp"/>

        <ImageView
            android:id="@+id/shareSong"
            android:layout_
            android:layout_
            android:src="@drawable/share_song"
            android:clickable="true"
            android:paddingTop="7dp"
            android:paddingLeft="130dp"/>



    </LinearLayout>

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_marginTop="150dp"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/durationTimer"
            android:layout_
            android:layout_
            android:textSize="70sp"
            android:textColor="#ffffff"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/playbackControls"
        android:layout_
        android:layout_
        android:layout_centerHorizontal="true"
        android:layout_marginTop="450dp"
        android:paddingTop="5dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/shuffleSong"
            android:layout_
            android:layout_
            android:src="@drawable/shuffle_song"
            android:clickable="true"
            android:padding="15dp"/>



        <ImageView
            android:id="@+id/previousImage"
            android:layout_
            android:layout_
            android:src="@drawable/previous_song"
            android:padding="15dp"
            android:clickable="true"/>


        <ImageView
            android:id="@+id/playpauseImage"
            android:layout_
            android:layout_
            android:padding="15dp"
            android:src="@drawable/play_song"
            android:clickable="true"/>

        <ImageView
            android:id="@+id/nextImage"
            android:layout_
            android:layout_
            android:padding="15dp"
            android:src="@drawable/next_song"
            android:clickable="true"/>

        <ImageView
            android:id="@+id/repeatSong"
            android:layout_
            android:layout_
            android:src="@drawable/repeat_song"
            android:clickable="true"
            android:padding="15dp"/>


    </LinearLayout>






</RelativeLayout>





</com.sothree.slidinguppanel.SlidingUpPanelLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:id="@+id/relativeLayout">






<li.yohan.parallax.ParallaxViewPager
    android:id="@+id/viewpager"
    android:layout_
    android:layout_
    android:background="@drawable/party"
    />







<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_
    android:layout_
    android:paddingTop="30dp">


    <TextView
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Musicana"
        android:typeface="sans"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:textSize="25sp" />



    <ImageView
        android:id="@+id/imageView"
        android:layout_
        android:layout_
        android:layout_above="@+id/viewpagertab"
        android:clickable="true"
        app:srcCompat="@drawable/ic_action_keyboard_arrow_right" />


</android.support.v7.widget.Toolbar>







<com.ogaclejapan.smarttablayout.SmartTabLayout
    android:id="@+id/viewpagertab"
    android:layout_
    android:layout_
    android:layout_below="@+id/toolbar"
    app:stl_indicatorAlwaysInCenter="false"
    app:stl_indicatorWithoutPadding="false"
    app:stl_indicatorInFront="false"
    app:stl_indicatorInterpolation="smart"
    app:stl_indicatorGravity="center"
    app:stl_indicatorColor="#6a000000"
    app:stl_indicatorThickness="30dp"
    app:stl_indicatorWidth="110dp"
    app:stl_indicatorCornerRadius="10dp"
    app:stl_overlineColor="#4D000000"
    app:stl_overlineThickness="0dp"
    app:stl_underlineColor="#ffffff"
    app:stl_underlineThickness="0dp"
    app:stl_dividerColor="#ffffff"
    app:stl_dividerThickness="0dp"
    app:stl_defaultTabBackground="?attr/selectableItemBackground"
    app:stl_defaultTabTextAllCaps="false"
    app:stl_defaultTabTextColor="#ffffff"
    app:stl_defaultTabTextSize="16sp"
    app:stl_defaultTabTextHorizontalPadding="30dp"
    app:stl_defaultTabTextMinWidth="0dp"
    app:stl_distributeEvenly="false"
    app:stl_clickable="true"
    app:stl_titleOffset="24dp"
    app:stl_drawDecorationAfterTab="false"
    app:stl_customTabTextLayoutId="@layout/custom_tab_with_typeface"
    app:stl_customTabTextViewId="@+id/custom_text"
    />

</RelativeLayout>

Logcat:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                        at com.musicplayer.musicana.BaseActivity.onCreate(BaseActivity.java:243)
                                                                        at com.musicplayer.musicana.MainActivity.onCreate(MainActivity.java:121)

【问题讨论】:

【参考方案1】:

我查看了您的代码,但在您在 BaseActivity.java 中投射的 base_activity.xml 中找不到任何用于后退箭头的 Imageview。检查 BaseActivity.java 中的以下行,您可能绑定了错误的 id,这就是您得到空指针异常的原因。

backarrow =  findViewById(R.id.imageView);

您的 base_activity 内容也不会显示在 MainActivity 中,只是因为您的 base_activity 内容替换为 main_activity 内容。

为了像这样在你的base_activity中添加一个视图(FrameLayout),就像this.where你的main_activity内容添加。

<FrameLayout
            android:id="@+id/container"
            android:layout_
            android:layout_/>

然后你的 MainActivity.java 不使用 setContentView() 方法删除它并使用下面的代码。它膨胀你的视图。就是这样,它的工作原理

  LayoutInflater inflater = LayoutInflater.from(this);
  View view = inflater.inflate(R.layout.activity_main, null);
  FrameLayout container = (FrameLayout) findViewById(R.id.container);
  container.addView(view);

【讨论】:

是的,对不起,我忘了提。你是对的,但现在 SlidingUpPanel 没有显示出来。任何想法,如何纠正它? @Rektirino 只是因为您的 BaseActivity 内容替换为您的 MainActivity 内容 我该怎么做?通过覆盖 BASEActivity 的 setCONtentView()? 好的,我明白了。如果我必须使用 MainActivity 和另外两个活动怎么办。我的意思是如果我想在另外两个活动中与 mainActivity 一起显示slidingUpPanel? 好的,我明白了。如果我必须使用 MainActivity 和另外两个活动怎么办。我的意思是如果我想在另外两个活动中与 mainActivity 一起显示slidingUpPanel?

以上是关于无法在所有活动中添加 SlidingUpPanel的主要内容,如果未能解决你的问题,请参考以下文章

Horizo​​ntalScrollView 在 SlidingUpPanel 中不起作用

无法在 SlidingUpPanelLayout Xamarin Android 中使用 ScrollView 布局

在 Android 中添加图像原因:无法恢复活动:未能交付结果 ResultInfowho=null, request=100, result=-1, data=null

在活动中添加/替换后,片段的所有元素都为空

在Android studio中使用活动模板时,我无法添加新的Java类吗?

Android:“BadTokenException:无法添加窗口;您的活动正在运行吗?”在 PreferenceActivity 中显示对话框