简体   繁体   中英

Android app crashes before requesting permissions

I'm trying to make an app that downloads something and then moves it.

The app works when I manually go into app preferences and give it permission, but I'm trying to give it permissions when the app opens.

Here is my code so far

package idkwhatimdoing.com.emojiupdaterroot;

import android.Manifest;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;

import java.io.IOException;
public class MainActivity extends AppCompatActivity {

    final Spinner emojiSpinner = (Spinner) findViewById(R.id.emojiSpinner);
    final String emojiSpinnerText = emojiSpinner.getSelectedItem().toString();
    final Button updateEmojiButton = (Button) findViewById(R.id.updateEmojiButton);
    // Assume thisActivity is the current activity

    private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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


        updateEmojiButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// Here, thisActivity is the current activity
                if (ContextCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {

                    // Should we show an explanation?
                    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                        // Show an expanation to the user *asynchronously* -- don't block
                        // this thread waiting for the user's response! After the user
                        // sees the explanation, try again to request the permission.

                    } else {

                        // No explanation needed, we can request the permission.

                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

                        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                        // app-defined int constant. The callback method gets the
                        // result of the request.
                    }
                }


            }
        });

    }
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    ProgressDialog progress = new ProgressDialog(MainActivity.this);
                    progress.setMessage("Downloading New Emojis");
                    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progress.setIndeterminate(true);
                    progress.show();
                    try {
                        Runtime.getRuntime().exec("su");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    if (emojiSpinnerText.equals("iPhone iOS 9.1")) {
                        String iosEmojiUrl = "http://idkwhatimdoing/androidapps/iPhone9_1.ttf";
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(iosEmojiUrl));
                        request.setTitle("Downloading_New_Emojis");
                        request.setDescription("iPhone iOS 9.1");
// in order for this if to run, you must use the android 3.2 to compile your app
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        }
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "NotoColorEmoji.ttf");

                        progress.setMessage("Updating Emojis");
// get download service and enqueue file
                        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        manager.enqueue(request);
                    } else if (emojiSpinnerText.equals("Android Marshmallow 6.0.1")) {
                        String iosEmojiUrl = "http://idkwhatimdoing/androidapps/Android6_0_1.ttf";
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(iosEmojiUrl));
                        request.setTitle("Downloading_New_Emojis");
                        request.setDescription("Android Marshmallow 6.0.1");
// in order for this if to run, you must use the android 3.2 to compile your app
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        }
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "NotoColorEmoji.ttf");

                        progress.setMessage("Updating Emojis");
// get download service and enqueue file
                        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        manager.enqueue(request);
                    }

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}

And My manifest has

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

Whenever I try to open the app, it just force closes.

Any ideas?

I'm testing this on Marshmallow 6.0.1

Here is my logcat

02-18 01:27:32.586 20844-20844/idkwhatimdoing.com.emojiupdaterroot E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: idkwhatimdoing.com.emojiupdaterroot, PID: 20844
                                                                                java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{idkwhatimdoing.com.emojiupdaterroot/idkwhatimdoing.com.emojiupdaterroot.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java)
                                                                                    at android.os.Looper.loop(Looper.java)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
                                                                                    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                                                    at android.app.Activity.findViewById(Activity.java)
                                                                                    at idkwhatimdoing.com.emojiupdaterroot.MainActivity.<init>(MainActivity.java:22)
                                                                                    at java.lang.Class.newInstance(Native Method)
                                                                                    at android.app.Instrumentation.newActivity(Instrumentation.java)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java) 
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java) 
                                                                                    at android.os.Looper.loop(Looper.java) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
                                                                                    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117) 

The method checkSelfPermission(String) was added to the API 23. Below API 23 it's of no use as the permission is always granted. Make a check for the API version before handling the permissions

int permissionCheck = 0;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
 permissionCheck  = ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
 if (Build.VERSION.SDK_INT >= 23) {

        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.e("testing", "Permission is granted");


        } else {


            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            Log.e("testing", "Permission is revoked");
        }
    } else { //permission is automatically granted on sdk<23 upon installation
        Log.e("testing", "Permission is already granted");

Grant permission in this way hope it may be help you

Try this ..

add theme in your MainActivity in manifest file like this android:theme="@style/Theme.AppCompat.Light.NoActionBar"

I don't know if you have found or not but issue is not Permission but following code.

final Spinner emojiSpinner = (Spinner) findViewById(R.id.emojiSpinner);
final String emojiSpinnerText = emojiSpinner.getSelectedItem().toString();
final Button updateEmojiButton = (Button) findViewById(R.id.updateEmojiButton);

You should assign values inside onCreate() method. Logcat clearly stated

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

You should have done following way:

final Spinner emojiSpinner;
final String emojiSpinnerText;
final Button updateEmojiButton;

and onCreate()

emojiSpinner = (Spinner) findViewById(R.id.emojiSpinner);
emojiSpinnerText = emojiSpinner.getSelectedItem().toString();
updateEmojiButton = (Button) findViewById(R.id.updateEmojiButton);

I added the following:

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
        //What you want to do when permissions are met
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM