Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

android - java.lang.NullPointerException (setWallpaper)

I didn't run into any problems in the tests. However, after launching the app, as the download occurs, many people are experiencing the phenomenon that the app is terminated with this error. Since the error was not implemented with my test phone, it is difficult to correct it. If you can help me, I would be very grateful.

Error Code

java.lang.NullPointerException: 
  at android.app.WallpaperManager.setBitmap (WallpaperManager.java:2462)
  at android.app.WallpaperManager.setBitmap (WallpaperManager.java:2374)
  at android.app.WallpaperManager.setBitmap (WallpaperManager.java:2347)
  at android.app.WallpaperManager.setBitmap (WallpaperManager.java:2313)
  at com.offsong.btsquotes.Full_screen.setWallaper (Full_screen.java:384)
  at com.offsong.btsquotes.Full_screen$5.onClick (Full_screen.java:196)
  at android.view.View.performClick (View.java:7870)
  at android.view.View.performClickInternal (View.java:7839)
  at android.view.View.access$3600 (View.java:886)
  at android.view.View$PerformClick.run (View.java:29363)
  at android.os.Handler.handleCallback (Handler.java:883)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:7811)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1068)

Full code

package com.offsong.enhypen;

import android.Manifest;
import android.app.DownloadManager;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.github.clans.fab.FloatingActionButton;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.IOException;

public class Full_screen extends AppCompatActivity {

    public static final String VIDEO_URL_KEY = "key";
    public static final String SINGER_KEY = "singer_key";
    public static final String VIDEO_TITLE_KEY = "title_key";
    public static final int STORAGE_PERMISSION_REQUEST_CODE = 1;
    MediaController mediaController;
    ImageView videoView;
    // ProgressDialog progressDialog;
    ProgressBar video_loading;
    Boolean isPLaying;
    int duration;
    int current;
    String URL;
    FloatingActionButton downloadBtn, shareBtn, repost, setwallpaper;
    String directoryName = "Status App";
    String title, finaltitle;
    Boolean per = false;
    File file;
    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen);
        mediaController = new MediaController(this);
        videoView = (ImageView) findViewById(R.id.full_screen_video_activty_id);

        video_loading = (ProgressBar) findViewById(R.id.videoLoading);
        downloadBtn = (FloatingActionButton) findViewById(R.id.download_ic);
        shareBtn = (FloatingActionButton) findViewById(R.id.share_ic);
        repost = (FloatingActionButton) findViewById(R.id.repost);
        setwallpaper = (FloatingActionButton) findViewById(R.id.setWallpaper);

        askPermissions();
        Intent intent = getIntent();


        URL = intent.getStringExtra(VIDEO_URL_KEY);
        MobileAds.initialize(this, getString(R.string.admob_app_id));
        AdView mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.banner_ad_unit_id_full));
        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);

                Log.d("ad", "onAdFailedToLoad" + " : " + String.valueOf(i));
            }

            @Override
            public void onAdLoaded() {

            }

            @Override
            public void onAdClosed() {

                AdRequest adRequest = new AdRequest.Builder().build();
                mInterstitialAd.loadAd(adRequest);
                setWallaper();

            }

        });


        title = intent.getStringExtra(VIDEO_TITLE_KEY);

        finaltitle = title + ".jpeg";


        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), finaltitle);


        if (URL != null) {


            Picasso.get().load(URL).into(videoView, new Callback() {
                @Override
                public void onSuccess() {
                    video_loading.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onError(Exception e) {

                }
            });


            repost.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    if (file.exists()) {

                        //File Exists};
                        Toast.makeText(getApplicationContext(), "Choose Any App", Toast.LENGTH_LONG).show();

                        shareIntent(file);

                    } else {


                        downloadFile(URL, finaltitle);
                        shareIntent(file);
                        //Toast.makeText(getApplicationContext(),"File is downloding, Click again once completed",Toast.LENGTH_LONG).show();


                    }


                }
            });


            downloadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (per) {

                        Toast.makeText(getApplicationContext(), "DOWNLOADING", Toast.LENGTH_LONG).show();
                        downloadFile(URL, finaltitle);

                        if (mInterstitialAd.isLoaded()) {
                            mInterstitialAd.show();
                        } else {
                            Log.d("TAG", "The interstitial wasn't loaded yet.");
                        }


                    } else {
                        Toast.makeText(getApplicationContext(), "ALLOW STORAGE ACCESS", Toast.LENGTH_LONG).show();

                    }

                }


            });


            setwallpaper.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (file.exists()) {
                        setWallaper();
                    } else {

                        downloadFile(URL, finaltitle);

                        Toast.makeText(getApplicationContext(), "It is applied after watching the advertisement.", Toast.LENGTH_SHORT).show();

                    }

                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    } else {
                        Log.d("TAG", "The interstitial wasn't loaded yet.");
                    }

                }


            });

            shareBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), finaltitle);
                    if (file.exists()) {

                        //File Exists};

                        shareIntent(file);

                    } else {


                        downloadFile(URL, finaltitle);
                        shareIntent(file);
                        //Toast.makeText(getApplicationContext(),"File is downloding, Click again once completed",Toast.LENGTH_LONG).show();


                    }

                }
            });


        }////////////ENDING
        else {

            Toast.makeText(getApplicationContext(), "Oops..", Toast.LENGTH_LONG).show();

        }


    }


    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStop() {
        super.onStop();


    }


    public void downloadFile(String url, String filename) {

        DownloadManager.Request dmr = new DownloadManager.Request(Uri.parse(url));


        String fileName = filename;
        dmr.setTitle(fileName);
        dmr.setDescription("Share The Quotes With Your Friends");
        dmr.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
        dmr.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        dmr.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(dmr);


    }


    public void shareIntent(File file) {

        String s = String.valueOf(file);


        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("video/*");
        String shareBodyText = "Download Status Videos ";
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(s));
        startActivity(Intent.createChooser(intent, "Choose sharing method"));


    }

    private void askPermissions() {

        int permissionCheckStorage = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        // we already asked for permisson & Permission granted, call camera intent
        if (permissionCheckStorage == PackageManager.PERMISSION_GRANTED) {

            //do what you want
            per = true;

        } else {

            // if storage request is denied
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("You need to give permission to access storage in order to work this feature.");
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...