java - FileProvider.getUriForFile 上下文抛出 NullPointer 异常
问题描述
我收到以下错误FileProvider.getUriForFile
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.content.Context.getResource()”
但是,当我显示时,context.toString()
我得到了这个(我认为)证明该对象不为空:
uk.co.letsdelight.letsdelight.MainActivity@8eec139
代码写入Completed download of update file
logcat 但不在URI successfully set
API 24 设备上写入。它适用于 API 23。这是代码的重要部分。该类是从withUpdate
调用的MainActivity
new Update(status, message).check(this);
public class Update extends Activity {
private File outputFile;
private TextView status;
private TextView message;
private String TAG = "LETSDELIGHT";
public Update(TextView status){
this.status = status;
}
public Update(TextView status, TextView message){
this.status = status;
this.message = message;
}
public void check(final Context c) {
status.setText("Checking for updates");
// Check for updates and give user option to update
// UPDATE button pressed
UpdateApp updateApp = new UpdateApp();
updateApp.setContext(c);
updateApp.execute(c.getString(R.string.apk_uri));
status.setText("Downloading update");
}
public class UpdateApp extends AsyncTask<String,Void,Void> {
private Context context;
private Throwable thrown = null;
private String statusMessage;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.connect();
Log.i(TAG,"Starting download of update file");
statusMessage = "Starting download of update file";
File file = context.getCacheDir();
file.mkdirs();
outputFile = new File(file, "update.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = conn.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
outputFile.setReadable(true, false);
Log.i(TAG, "Completed download of update file");
statusMessage = "Completed download of update file";
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, getString(R.string.file_provider_authority), outputFile);
} else {
uri = Uri.fromFile(outputFile);
}
Log.i(TAG, "URI successfully set");
statusMessage = "URI successfully set";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (Throwable ex) {
Log.e(TAG, ex.toString());
thrown = ex;
}
return null;
}
@Override
protected void onPostExecute(Void v) {
status.setText("");
if (statusMessage != null) {
status.setText(statusMessage);
}
if (thrown != null) {
status.setText(context.toString());
message.setText(thrown.toString());
}
outputFile.delete();
super.onPostExecute(v);
}
}
}
该AndroidManifest.xml
文件包含以下条目:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="@strings/file_provider_authority"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
file_provider_paths.xml
看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="."/>
</paths>
我已经尝试更改context
为两者getApplication()
,并且getApplicationContext()
两者都导致NullPointerException
但资源措辞略有不同。
我还能尝试什么来解决这个问题?
编辑:
我已经修改了代码以尝试找出问题的根源:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
statusMessage = "Context is: " + context.getString(R.string.file_provider_authority);
uri = FileProvider.getUriForFile(context, context.getString(R.string.file_provider_authority), outputFile);
statusMessage = "uri set";
} else {
uri = Uri.fromFile(outputFile);
}
statusMessage
设置为显示正确的提供者权限字符串。它没有设置为“uri set”。因此,它肯定FileProvider.getUriForFile()
是失败的。
鉴于它context
不为空并且授权是正确的字符串,还有什么可能在这里引发错误?
解决方案
您正在尝试在非活动类中调用 getString 尝试替换
getString(R.string.file_provider_authority)
和
context.getString(R.string.file_provider_authority)
Update
编辑:我看到你在你的班级中扩展活动你在清单中声明了吗?如果您已经在传递上下文,为什么还要扩展活动?
推荐阅读
- javascript - event.preventDefault() 在 contenteditable div 中似乎对我不起作用
- javascript - D3.js 代码不适用于根据数据创建圆圈
- r - ChordDiagram 切断长标签
- sapui5 - 如何将 headerText 居中对齐?
- node.js - 如何将 ClojureScript 节点 REPL 连接到我的 :node-library shadow-cljs 项目?
- haskell - 使用存在类型 Haskell 时出现类型错误
- cvs2git - cvs2git 在 miration 期间丢失了一些提交
- mysql - Google Cloud Platform - 在一个项目中使用 mysql 实例到另一个项目 - 将一个项目中的 mysql 实例连接到另一个项目
- regex - 使用正则表达式验证掩码和限制货币范围
- php - 在 Shortcode 中更改产品类别的 WooCommerce 添加到购物车消息