我正在使用Wp rest api将应用程序制作为Wordpress后端,但我遇到一个问题,首先尝试加载11张图像,当我全屏打开图像时,它可以正常工作到第9张图像,并滑动到第10张图像时显示错误加载失败,并且在下一张幻灯片应用程序停止了。。这是错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Integer packegename.model.wallpaper.Wallpaper.getId()' on a null object reference
at packegename.activity.FullscreenActivity.setFabFavoriteIcon(FullscreenActivity.java:176)
at packegename.activity.FullscreenActivity.updateInfo(FullscreenActivity.java:172)
at packegename.activity.FullscreenActivity.access$100(FullscreenActivity.java:32)
at com.mcc.wallpaper.activity.FullscreenActivity$1.onPageSelected(FullscreenActivity.java:118)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1941)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:680)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:664)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2257)
at android.view.View.dispatchTouchEvent(View.java:10060)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2627)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2322)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:414)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
at android.app.Activity.dispatchTouchEvent(Activity.java:3108)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:376)
at android.view.View.dispatchPointerEvent(View.java:10280)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4449)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4317)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3917)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3878)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4010)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3886)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4067)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3917)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3878)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3886)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6262)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6232)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6193)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6365)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:189)
at android.os.MessageQueue.nativePollOnce(Native Method)
这是全屏活动
public class FullscreenActivity extends BaseActivity {
// variable
private Context mContext;
private Activity mActivity;
private WallpaperSliderAdapter sliderAdapter;
private ArrayList<Wallpaper> wallpapers;
private String wallpaperType;
private int currentPosition;
// constant
private static final int MY_PERMISSIONS_REQUEST_WRITE_STORAGE = 1001;
// ui elements
private ViewPager vpWallpaperSlider;
private TextView tvWallpaperCount;
private FloatingActionMenu fabMenu;
private FloatingActionButton fabShareWallpaper;
private FloatingActionButton fabFavoriteWallpaper;
private FloatingActionButton fabDownloadWallpaper;
private FloatingActionButton fabSetWallpaper;
private FavDbController favDbController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
initVariable();
initView();
initListener();
}
private void initVariable() {
mContext = FullscreenActivity.this;
mActivity = FullscreenActivity.this;
if (getIntent().hasExtra(AppConstants.KEY_WALLPAPERS)) {
wallpapers = getIntent().getParcelableArrayListExtra(AppConstants.KEY_WALLPAPERS);
}
if (getIntent().hasExtra(AppConstants.KEY_POSITION)) {
currentPosition = getIntent().getIntExtra(AppConstants.KEY_POSITION, 0);
}
if (getIntent().hasExtra(AppConstants.KEY_WALLPAPER_TYPE)) {
wallpaperType = getIntent().getStringExtra(AppConstants.KEY_WALLPAPER_TYPE);
}
sliderAdapter = new WallpaperSliderAdapter(getSupportFragmentManager(), wallpapers, wallpaperType);
favDbController = new FavDbController(mContext);
}
private void initView() {
setContentView(R.layout.activity_fullscreen);
initToolbar();
enableBackButton();
vpWallpaperSlider = findViewById(R.id.vpWallpaperSlider);
tvWallpaperCount = findViewById(R.id.tvWallpaperCount);
fabMenu = findViewById(R.id.fabMenu);
fabShareWallpaper = findViewById(R.id.fab_share_wallpaper);
fabFavoriteWallpaper = findViewById(R.id.fab_favorite_wallpaper);
fabDownloadWallpaper = findViewById(R.id.fab_download_wallpaper);
fabSetWallpaper = findViewById(R.id.fab_set_wallpaper);
vpWallpaperSlider.setAdapter(sliderAdapter);
vpWallpaperSlider.setCurrentItem(currentPosition);
updateInfo(currentPosition);
}
private void initListener() {
vpWallpaperSlider.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
currentPosition = position;
updateInfo(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
fabShareWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
shareWallpaper();
fabMenu.close(true);
}
});
fabFavoriteWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addTofavoriteWallpaper();
setFabFavoriteIcon();
fabMenu.close(true);
}
});
fabDownloadWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_STORAGE);
} else {
saveWallpaper();
fabMenu.close(true);
}
}
});
fabSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setWallpaper();
fabMenu.close(true);
}
});
}
private void updateInfo(int position) {
tvWallpaperCount.setText(StringUtil.addLeadingZero((position + 1)) + "/" + StringUtil.addLeadingZero(wallpapers.size()));
setFabFavoriteIcon();
}
private void setFabFavoriteIcon() {
if (favDbController.isFavorite(wallpapers.get(currentPosition).getId())) {
fabFavoriteWallpaper.setImageResource(R.drawable.ic_favorite_orange_solid);
} else {
fabFavoriteWallpaper.setImageResource(R.drawable.ic_favorite_orange_border);
}
}
private void shareWallpaper() {
Fragment fragment = sliderAdapter.getFragment(vpWallpaperSlider.getCurrentItem());
if (fragment != null) {
if (wallpaperType.equals(AppConstants.TYPE_WALLPAPER)) {
((WallpaperSliderFragment) fragment).shareWallpaper();
} else {
((GifSliderFragment) fragment).shareWallpaper();
}
}
}
private void addTofavoriteWallpaper() {
Fragment fragment = sliderAdapter.getFragment(vpWallpaperSlider.getCurrentItem());
if (fragment != null) {
if (wallpaperType.equals(AppConstants.TYPE_WALLPAPER)) {
((WallpaperSliderFragment) fragment).addToFavoriteWallpaper();
} else {
((GifSliderFragment) fragment).addToFavoriteWallpaper();
}
}
}
private void saveWallpaper() {
Fragment fragment = sliderAdapter.getFragment(vpWallpaperSlider.getCurrentItem());
if (fragment != null) {
if (wallpaperType.equals(AppConstants.TYPE_WALLPAPER)) {
((WallpaperSliderFragment) fragment).saveWallpaper();
} else {
((GifSliderFragment) fragment).saveWallpaper();
}
}
}
private void setWallpaper() {
Fragment fragment = sliderAdapter.getFragment(vpWallpaperSlider.getCurrentItem());
if (fragment != null) {
if (wallpaperType.equals(AppConstants.TYPE_WALLPAPER)) {
((WallpaperSliderFragment) fragment).setWallpaper();
} else {
((GifSliderFragment) fragment).setWallpaper();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveWallpaper();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(getString(R.string.storage_permission_required_to_download));
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return super.onSupportNavigateUp();
}
}