我正在尝试通过创建baseActivity在所有活动中从umano(https://github.com/umano/AndroidSlidingUpPanel)实现SlidingUpPanel。但是在我创建了所有内容(baseactivity.java和baseactivity.xml)并将我的MainActivity.java扩展为baseactivity后,它在BaseActivity中出现的每个视图上都给出了nullpointerException。我似乎无法弄清楚为什么。 MainActivity有自己独立的.xml,我只想添加SlidingUpPanel而不会干扰MainActivity的现有布局。
我知道是什么原因导致空指针异常,但在这里我已经初始化了每个视图。帮助我。
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_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>
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_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout">
<li.yohan.parallax.ParallaxViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/party"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="48dp"
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)
答案 0 :(得分:1)
我查看了您的代码,但在您在BaseActivity.java中强制转换的base_activity.xml中找不到任何有关backarrow的Imageview。检查BaseActivity.java中的以下行,你可能绑定了错误的id,这就是你得到空指针异常的原因。
backarrow = findViewById(R.id.imageView);
此外,您的base_activity内容不会显示在MainActivity中,只是因为您的base_activity内容被main_activity内容替换。
为了达到这样的目的,在你的base_activity中添加一个View(FrameLayout),就像这样。你的main_activity内容添加。
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后你的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);