英文:
How to move from one fragment to another
问题
在两行星号之间的代码部分,您尝试使用以下代码切换到第二个片段。但是,您遇到了空引用异常(NullPointerException)。
DisplayFragment displayFragment = new DisplayFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.DisplayFragment, displayFragment, displayFragment.getTag()).commit();
detectTextFromImage();
要解决这个问题,您需要确保以下几点:
- 确保您的布局中包含一个具有id为
DisplayFragment
的FrameLayout,用于显示第二个片段。 - 确保
DisplayFragment
类和相关的布局文件存在。 - 在
detectTextFromImage
方法中,确保imageBitmap
不为空,否则在调用FirebaseVisionImage.fromBitmap(imageBitmap)
之前检查它。
如果您仍然遇到问题,可以提供更多关于 DisplayFragment
和布局文件的信息,以便更详细地帮助您解决问题。
英文:
I currently have a program that has multiple fragments, and I want a button in the home fragment to take me to a second fragment. I cannot figure out how to do this. I will include the code for my HomeFragment
HomeFragment.java
package com.mvsolutions.snap.ui.home;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.text.FirebaseVisionText;
import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;
import com.mvsolutions.snap.R;
import com.mvsolutions.snap.ui.display.DisplayFragment;
import java.util.List;
import java.util.zip.Inflater;
import static android.app.Activity.RESULT_OK;
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private ImageView imageView;
private Button captureImageButton, detectButton, pickButton;
private Bitmap imageBitmap;
private TextView capturedTextView;
static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int RESULT_LOAD_IMAGE = 101;
public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
//View v = inflater.inflate(R.layout.fragment_display, container, false);
imageView = root.findViewById(R.id.home_image_view_img);
capturedTextView = root.findViewById(R.id.home_text_view_txt);
captureImageButton = root.findViewById(R.id.capture_image_btn);
detectButton = root.findViewById(R.id.detect_text_btn);
pickButton = root.findViewById(R.id.pick_image_btn);
captureImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
captureImage();
}
});
pickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickImage();
}
});
detectButton.setOnClickListener(new View.OnClickListener() {
@Override
*********************************************************************************************
public void onClick(View v) {
DisplayFragment displayFragment = new DisplayFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.DisplayFragment, displayFragment, displayFragment.getTag()).commit();
*********************************************************************************************
detectTextFromImage();
}
});
return root;
}
private void detectTextFromImage() {
capturedTextView.setText("");
FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);
FirebaseVisionTextDetector visionTextDetector = FirebaseVision.getInstance().getVisionTextDetector();
visionTextDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
List<FirebaseVisionText.Block> textBlocks = firebaseVisionText.getBlocks();
if (textBlocks.size() == 0) {
Toast.makeText(getContext(), "No Text Found", Toast.LENGTH_SHORT).show();
} else {
for (FirebaseVisionText.Block block : textBlocks) {
String text = block.getText();
capturedTextView.setText(capturedTextView.getText() + " " + text);
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
Log.d("Error", e.getMessage());
}
});
}
private void pickImage() {
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
private void captureImage() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
between the two lines of asterisks, I have attempted to write some code to make the button take me to the second fragment. If anyone can fix this code, or offer me a new solution towards answering my question, that would be greatly appreciated.
I had forgotten to add a commit statement in my code but even with that it still crashes. Here is the error message:
2020-08-01 13:55:23.904 22878-22878/com.mvsolutions.snap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mvsolutions.snap, PID: 22878
java.lang.NullPointerException: null reference
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:7)
at com.google.firebase.ml.vision.common.FirebaseVisionImage.<init>(Unknown Source:3)
at com.google.firebase.ml.vision.common.FirebaseVisionImage.fromBitmap(Unknown Source:2)
at com.mvsolutions.snap.ui.home.HomeFragment.detectTextFromImage(HomeFragment.java:94)
at com.mvsolutions.snap.ui.home.HomeFragment.access$200(HomeFragment.java:40)
at com.mvsolutions.snap.ui.home.HomeFragment$3.onClick(HomeFragment.java:86)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14966)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29315)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7777)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
2020-08-01 13:55:23.923 22878-22878/com.mvsolutions.snap I/Process: Sending signal. PID: 22878 SIG: 9
答案1
得分: 1
You are missing a call to FragmentTransaction.commit().
public void onClick(View v) {
DisplayFragment displayFragment = new DisplayFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.DisplayFragment, displayFragment, displayFragment.getTag())
.commit() // <- HELLO!;
Edit: Well, now you're just asking a completely new question, but OK.
Your error message is pretty explicit: you are passing null to FirebaseVisionImage.fromBitmap
in your call to detectTextFromImage
in your HomeFragment
at line 94:
java.lang.NullPointerException: null reference
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:7)
at com.google.firebase.ml.vision.common.FirebaseVisionImage.
at com.google.firebase.ml.vision.common.FirebaseVisionImage.fromBitmap(Unknown Source:2)
at com.mvsolutions.snap.ui.home.HomeFragment.detectTextFromImage(HomeFragment.java:94)
From your code, I'm guessing it's imageBitmap
so you should start there. Please use your debugger to determine why that is null, then fix your logic so it's not null.
英文:
You are missing a call to FragmentTransaction.commit().
*********************************************************************************************
public void onClick(View v) {
DisplayFragment displayFragment = new DisplayFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.DisplayFragment, displayFragment, displayFragment.getTag())
.commit() // <- HELLO!;
*********************************************************************************************
<hr />
Edit: Well, now you're just asking a completely new question, but OK.
Your error messages is pretty explicit: you are passing null to FirebaseVisionImage.fromBitmap
in your call to detectTextFromImage
in your HomeFragment
at line 94:
java.lang.NullPointerException: null reference
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:7)
at com.google.firebase.ml.vision.common.FirebaseVisionImage.<init>(Unknown Source:3)
at com.google.firebase.ml.vision.common.FirebaseVisionImage.fromBitmap(Unknown Source:2)
at com.mvsolutions.snap.ui.home.HomeFragment.detectTextFromImage(HomeFragment.java:94)
From your code I'm guessing it's imageBitmap
so you should start there. Please use your debugger to determine why that is null then fix your logic so it's not null.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论