gpt4 book ai didi

安卓运行时权限

转载 作者:行者123 更新时间:2023-11-29 15:42:28 24 4
gpt4 key购买 nike

我正在尝试添加运行时权限以使应用与 API 23+ 兼容。我浏览了 documentation但是当我只包含以下行时

private final int hasWriteFilePermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);,

我的应用程序崩溃并出现以下错误:

07-10 20:54:15.571 31479-31479/com.ibm.watson.developer_cloud.android.examples E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ibm.watson.developer_cloud.android.examples, PID: 31479
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ibm.watson.developer_cloud.android.examples/com.ibm.watson.developer_cloud.android.examples.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:115)
at android.widget.Spinner.setAdapter(Spinner.java:554)
at com.ibm.watson.developer_cloud.android.examples.MainActivity$FragmentTabSTT.addItemsOnSpinnerModels(MainActivity.java:566)
at com.ibm.watson.developer_cloud.android.examples.MainActivity$FragmentTabSTT.onCreateView(MainActivity.java:190)
at android.app.Fragment.performCreateView(Fragment.java:2281)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:983)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1163)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1552)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6914)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3217)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我的代码如下:

public static class FragmentTabSTT extends Fragment implements ISpeechToTextDelegate {
.
.
.
@Override
public void onAttach(Context context) {
super.onAttach(context);

Activity a;

if (context instanceof Activity){
a = (Activity) context;
}

}



public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
.
.
.
}
.
.
.
// One method that requires permission
public ArrayList<String> GetFiles(String DirectoryPath) {

int hasReadFilesPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);

if (hasReadFilesPermission != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(permission.READ_EXTERNAL_STORAGE)) {
showMessageOKCancel("You must give permission to read from storage.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
}
});
return null;
}

requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return null;
}
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(DirectoryPath);

f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++)
MyFiles.add(files[i].getName());
}

return MyFiles;
}
}

我应该如何检查 fragment 中的权限?是什么导致了这个错误?我该如何解决这个问题?谢谢!

最佳答案

getActivity() 在首次创建 Fragment 时返回 null(即,在调用 onAttach() 之前)。

您应该在需要之前直接检查权限,而不是将它们保存在最终类变量中。

关于安卓运行时权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com