Android Can';t在所有活动中添加SlideingPanel
我试图通过创建baseActivity,在所有活动中实现来自umano()的SlidingUpPanel。但是在我创建了所有内容(baseactivity.java和baseactivity.xml)并将MainActivity.java扩展到baseactivity之后,它在baseactivity中存在的每个视图上都给了我nullpointerException。我似乎不明白为什么。MainActivity有自己独立的.xml,我只想在不影响MainActivity现有布局的情况下向其添加SlidengPanel 我知道是什么导致空指针异常,但在这里我初始化了每个视图。帮帮我 BaseActivity.java:Android Can';t在所有活动中添加SlideingPanel,android,inheritance,android-activity,nullpointerexception,Android,Inheritance,Android Activity,Nullpointerexception,我试图通过创建baseActivity,在所有活动中实现来自umano()的SlidingUpPanel。但是在我创建了所有内容(baseactivity.java和baseactivity.xml)并将MainActivity.java扩展到baseactivity之后,它在baseactivity中存在的每个视图上都给了我nullpointerException。我似乎不明白为什么。MainActivity有自己独立的.xml,我只想在不影响MainActivity现有布局的情况下向其添加S
@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);
}
}
});
}
@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();
}
MainActivity.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);
}
}
});
}
@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_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoPanelHeight="70dp"
sothree:umanoShadowHeight="10dp"
android:background="#b9ffffff">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</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_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"
android:orientation="horizontal"
tools:context="com.musicplayer.musicana"
>
<ImageView
android:id="@+id/background_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/party"
android:scaleType="centerCrop"/>
<LinearLayout
android:id="@+id/newlr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bd3b3838"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="15dp"
>
<TextView
android:id="@+id/SongNameSlide"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Closer"
android:textSize="15sp"
android:textColor="#ffffff"
android:paddingLeft="20dp"/>
<TextView
android:id="@+id/ArtistNameSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Chainsmokers"
android:textSize="10sp"
android:textColor="#ffffff"
android:paddingLeft="20dp"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/songButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="250dp"
android:layout_height="250dp"
android:layout_marginTop="85dp"
android:layout_centerHorizontal="true"/>
<me.tankery.lib.circularseekbar.CircularSeekBar
android:id="@+id/circularSeekBar"
android:layout_width="300dp"
android:layout_height="300dp"
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_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="390dp"
android:layout_centerHorizontal="true"
>
<TextView
android:id="@+id/fullSongName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:textSize="16sp"
android:textColor="#ffffff"
android:text="SongName appears here"/>
<TextView
android:id="@+id/fullArtistName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/downArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:paddingLeft="5dp"
android:clickable="true"
android:src="@drawable/down_arrow"/>
<TextView
android:id="@+id/nowPlaying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Now Playing"
android:textColor="#ffffff"
android:textSize="20sp"
android:paddingLeft="20dp"
android:paddingTop="10dp"/>
<ImageView
android:id="@+id/shareSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/share_song"
android:clickable="true"
android:paddingTop="7dp"
android:paddingLeft="130dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/durationTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="70sp"
android:textColor="#ffffff"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/playbackControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:paddingTop="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/shuffleSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shuffle_song"
android:clickable="true"
android:padding="15dp"/>
<ImageView
android:id="@+id/previousImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/previous_song"
android:padding="15dp"
android:clickable="true"/>
<ImageView
android:id="@+id/playpauseImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:src="@drawable/play_song"
android:clickable="true"/>
<ImageView
android:id="@+id/nextImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:src="@drawable/next_song"
android:clickable="true"/>
<ImageView
android:id="@+id/repeatSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/repeat_song"
android:clickable="true"
android:padding="15dp"/>
</LinearLayout>
</RelativeLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
我查看了您的代码,在您在BaseActivity.java中强制转换的base_activity.xml中找不到任何backarrow的Imageview。检查BaseActivity.java中的下一行,您可能绑定了错误的id,这就是为什么会出现空指针异常
backarrow = findViewById(R.id.imageView);
此外,您的基本活动内容不会显示在主活动中,因为您的基本活动内容被主活动内容替换
要实现此目的,请在基本活动中添加一个视图(FrameLayout),如下所示。主活动内容将添加到其中
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
是的,对不起,我忘了提那件事。您是对的,但是现在没有显示SlideingPanel。有什么想法吗?怎么更正?@Rektirino只是因为你的基本活动内容被你的主要活动内容替换了,我怎么更正?通过重写BAseActivity的setCOntentView()?好的,我看到了。如果我必须使用MainActivity和另外两个Activite怎么办。我的意思是,如果我想在另外两个活动中与mainActivity一起显示SlideingPanel呢?好的,我看到了。如果我必须使用MainActivity和另外两个Activite怎么办。我的意思是,如果我想在另外两个活动中与mainActivity一起显示SlideingPanel,该怎么办?
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.activity_main, null);
FrameLayout container = (FrameLayout) findViewById(R.id.container);
container.addView(view);