我遇到此错误: java.lang.NullPointerException:
尝试在java.lang.String android.net.Uri.toString()
上调用虚拟方法null object reference
android studio监视器在说:
2018-09-30 10:51:38.694 14302-14302/com.photoshare.shareyourfeelings E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.photoshare.shareyourfeelings, PID: 14302
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at com.photoshare.shareyourfeelings.SetupActivity.storeFirestore(*SetupActivity.java*:236)
at com.photoshare.shareyourfeelings.SetupActivity.access$1000(*SetupActivity.java*:47)
at com.photoshare.shareyourfeelings.SetupActivity$2$1.onComplete(*SetupActivity.java*:164)
at com.google.firebase.storage.StorageTask.zza(com.google.firebase:firebase-storage@@16.0.2:143)
at com.google.firebase.storage.zzp.zza(Unknown Source:4)
at com.google.firebase.storage.zzv.zza(com.google.firebase:firebase-storage@@16.0.2:89)
at com.google.firebase.storage.zzy.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
2018-09-30 10:52:29.874 14302-14364/com.photoshare.shareyourfeelings I/Adreno: DequeueBuffer: dequeueBuffer failed
2018-09-30 10:52:29.876 14302-14364/com.photoshare.shareyourfeelings I/Adreno: DequeueBuffer: dequeueBuffer failed
2018-09-30 10:52:29.876 14302-14364/com.photoshare.shareyourfeelings I/Adreno: DequeueBuffer: dequeueBuffer failed
2018-09-30 10:52:29.881 14302-14364/com.photoshare.shareyourfeelings W/OpenGLRenderer: swapBuffers encountered EGL error 12301 on 0x7d3f6459c0, halting rendering...
SetupActivity.java
public class SetupActivity extends AppCompatActivity {
private CircleImageView setupImage;
private Uri mainImageURI = null;
private Uri download_uri;
private String user_id;
private boolean isChanged = false;
private EditText setupName;
private Button setupBtn;
private ProgressBar setupProgress;
private StorageReference storageReference;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
private Bitmap compressedImageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
Toolbar setupToolbar = findViewById(R.id.setupToolbar);
setSupportActionBar(setupToolbar);
getSupportActionBar().setTitle("Account Name Setup");
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
if(task.getResult().exists()){
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.default_image);
Glide.with(SetupActivity.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = setupName.getText().toString();
Log.d("LogInformation", user_name);
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
setupProgress.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageURI.getPath());
try {
compressedImageFile = new Compressor(SetupActivity.this)
.setMaxHeight(125)
.setMaxWidth(125)
.setQuality(50)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();
UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
image_path.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
storeFirestore(task, user_name);
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, " There was an error : " + error, Toast.LENGTH_LONG).show();
setupProgress.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name);
}
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(SetupActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
Toast.makeText(SetupActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(SetupActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFirestore(@NonNull Task<UploadTask.TaskSnapshot> task, String user_name) {
if(task != null) {
com.google.firebase.storage.UploadTask.TaskSnapshot download_uri;
download_uri = task.getResult();
} else {
download_uri = mainImageURI;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("name", user_name);
userMap.put("image", download_uri.toString());
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(SetupActivity.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(SetupActivity.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
答案 0 :(得分:0)
这是android版本的问题
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M){
final Uri data = intent.getData();
final File file = new File(data.getPath());
// your code
}else{
// your code
}