我正在尝试从对话框片段中启动ACTION_IMAGE_CAPTURE活动,并在单击相关按钮以启动它时获得NPE。
我检查了清单权限,并验证了我的片段中的权限字符串具有与清单相同的“ .fileprovider”。
private static final int REQUEST_CODE = 123;
private EditText etUsername, etPassword;
private Button btnSignup;
private TextView tvGoback;
private String username, password;
private OnSignupFragmentListener listener;
private ImageView selfiImage;
private String photoPath;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setListener(OnSignupFragmentListener listener) {
this.listener = listener;
}
@Override
public void onStart() {
super.onStart();
Objects.requireNonNull(getDialog().getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().setTitle("Sign-up");
getDialog().getWindow().setBackgroundDrawableResource(R.drawable.signup_background);
View view = inflater.inflate(R.layout.fragment_signup, container, false);
etUsername = view.findViewById(R.id.etUsername);
etPassword = view.findViewById(R.id.etPassword);
btnSignup = view.findViewById(R.id.btnSignup);
tvGoback = view.findViewById(R.id.tvGobacktologin);
selfiImage = view.findViewById(R.id.ivSelfi);
btnSignup.setOnClickListener(this);
tvGoback.setOnClickListener(this);
selfiImage.setOnClickListener(this);
if (username != null) {
etUsername.setText(username);
}
if (password != null) {
etPassword.setText(password);
}
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnSignup:
signup();
break;
case R.id.tvGoBack:
dismiss();
break;
case R.id.ivSelfi:
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePhotoIntent.resolveActivity(getContext().getPackageManager()) != null) {
File file = createImageFile();
Uri photoUri = FileProvider.getUriForFile(getContext(), "com.example.alon.a2018_17_12_userloginexhomework.fileprovider", file);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePhotoIntent, REQUEST_CODE);
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == LoginActivity.RESULT_OK){
setPic();
}
}
private void setPic(){
int imageWidth = selfiImage.getWidth();
int imageHeight = selfiImage.getHeight();
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, bitmapOptions);
int photoWidth = bitmapOptions.outWidth;
int photoHeight = bitmapOptions.outHeight;
int scaleFactor = Math.min(photoWidth / imageWidth, photoHeight / imageHeight);
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions);
selfiImage.setImageBitmap(bitmap);
}
private void signup() {
String userName = etUsername.getText().toString();
String password = etPassword.getText().toString();
if (userName.isEmpty() || password.isEmpty()) {
Toast.makeText(getContext(), "username and password are required", Toast.LENGTH_SHORT).show();
return;
}
if (listener != null)
listener.onSignup(userName, password);
dismiss();
}
private File createImageFile(){
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = new File(storageDir, "photo.jpg");
photoPath = file.getAbsolutePath();
return file;
}
public interface OnSignupFragmentListener {
void onSignup(String username, String password);
}
s
单击ivSelfi按钮时,我得到以下NPE-
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:605)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:579)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.example.alon.a2018_17_12_userloginexhomework.SignupFragment.onClick(SignupFragment.java:92)
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>
我的清单文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="false"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.camera.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
<activity android:name=".GameActivity"></activity>
<activity android:name=".LoginActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UserDetailsActivity" />
</application>
我的file_paths.xml文件-
<paths>
<external-path
name="my_images"
path="Android/data/com.example.alon.a2018_17_12_userloginexhomework/files/Pictures" />
答案 0 :(得分:1)
好的,所以我想出了解决方案-我写的软件包名称不正确。该项目是:
android:authorities="com.example.alon.a2018_17_12_userloginexhomework.fileprovider"