需要帮助,在AlertDialog.Builder中的getPackageName()处崩溃

时间:2018-03-13 07:20:41

标签: java android alertdialog

我试图从位于 FilesActivity.java 中的showAlertForPermission访问onRequestPermissionsResult,但它在行

处提供nullpointerexception
 Uri uri = Uri.fromParts("package", getPackageName(), null);

在ConnectionActivity.java中:

public void showAlertForPermission (Context context){
        AlertDialog.Builder NeverPopUp =new AlertDialog.Builder(context);
        NeverPopUp.setMessage("To continue, allow \"iXm Uplink\" access to your device's audio files. Tap Settings > Permissions, and turn Storage on.")
                .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //settings
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Exit
                    }
                })
                .show();
    }

在FilesActivity.java中:

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.v("Storage: ","Storage permissions Granted");
                } else {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(FilesActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        //Shows permission explanation using dialog window
                    } else {
                        connectedActivity.showAlertForPermission(FilesActivity.this);

                    }
                }
            }
        }
    }

错误

 E/AndroidRuntime: FATAL EXCEPTION: main        
                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                          at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
                          at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

经过一番搜索,我试过了:

  

Intent intent = new Intent(activity,FileActivity.class);

错误确实退出,我做错了什么?

3 个答案:

答案 0 :(得分:0)

您可能想尝试context.getPackageName() .. context是您传递给该方法的变量。

答案 1 :(得分:0)

这是因为您从ConnectionActivity.java类调用了showAlertForPermission,但您还没有创建ConnectionActivity对象。通过抛出空指针异常,它肯定会使程序崩溃。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1: {
          ...
          // This will throw error because you
          connectedActivity.showAlertForPermission(FilesActivity.this);

        }
    }
}

在我看来,您正在尝试使用可重用的方法来显示警报对话框。所以,你可以像这样公开showAlertForPermission

public static void showAlertForPermission (Context context) {
  ...
}

然后你可以像这样使用它:

 ConnectionActivity.showAlertForPermission(FilesActivity.this);

答案 2 :(得分:0)

我更喜欢使用

BuildConfig.APPLICATION_ID

但是首先,请确保您的应用程序ID是正确的。 因为有时它不指代您的应用程序ID,而可能指代库ID或其他名称。 如果是这样,请清理并再次重建项目,并确保您导入正确的Bui​​ldConfig文件,因为您可能会导入项目中使用的库的BuildConfig文件