图书馆由于许可而无法正常工作

时间:2018-11-22 11:22:22

标签: android permissions android-library

我正在尝试将现有项目转换为android中的库。我的代码在项目中运行,但是在库中不起作用。 我的项目需要File_Read和Write权限。我尝试了所有组合。错误与context.permission或res.assetManager或文件描述符有关。请帮助

public class Kon extends AppCompatActivity {

private static final int REQUEST_ID_READ_PERMISSION = 100;
private static final int REQUEST_ID_WRITE_PERMISSION = 200;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    askPermissionAndReadFile();
}
.....
.....
private void askPermissionAndReadFile()
private void askPermissionAndWriteFile()
....
private boolean askPermission(int requestId, String permissionName) {
    if (android.os.Build.VERSION.SDK_INT >= 23) {

        int permission = ActivityCompat.checkSelfPermission(this, permissionName);

        if (permission != PackageManager.PERMISSION_GRANTED) {
                    this.requestPermissions(
                    new String[]{permissionName},
                    requestId
            );
            return false;
        }
    }
    return true;
    }

  void ComFile(String kel[]) {
  ...... 
  ......
  try {
        for (int i = 0; i < fs.length; i++) {

            Context context=getApplicationContext();
            AssetManager am=context.getAssets();
            AssetFileDescriptor fileDescriptor = am.openFd(kel[i].toLowerCase());
            fileDescriptor = am.openFd(kel[i].toLowerCase());
            fs[i] = fileDescriptor.createInputStream();


        }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

0 个答案:

没有答案