0

我正在尝试从我的对话框片段启动 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" />

4

1 回答 1

1

好的,所以我想出了解决方案 - 我写的包名不正确。正确的是:

android:authorities="com.example.alon.a2018_17_12_userloginexhomework.fileprovider"
于 2019-01-25T10:25:29.673 回答