我正在尝试将现有项目转换为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