I'm developing an Android app for gender recognition.
Now I want to access the classification result saved as a non-static String, public String GENDER
, in public class Camera2BasicFragment extends Fragment implements FragmentCompat.OnRequestPermissionsResultCallback
. I want to access it from MainActivity
class.
I have a classification method in my Camera2BasicFragment
class.
private void classifyFrame() {
if (gclassifier == null || eclassifier == null || getActivity() == null || cameraDevice == null) {
showToast("Uninitialized Classifier or invalid context.");
return;
}
Bitmap bitmap_g =
textureView.getBitmap(GenderClassifier.DIM_IMG_SIZE_X, GenderClassifier.DIM_IMG_SIZE_Y);
Bitmap bitmap_e =
textureView.getBitmap(EmotionClassifier.DIM_IMG_SIZE_X, EmotionClassifier.DIM_IMG_SIZE_Y);
GENDER = gclassifier.classifyFrame(bitmap_g);
EMOTION = eclassifier.classifyFrame(bitmap_e);
String textToShow = GENDER + EMOTION;
bitmap_g.recycle();
bitmap_e.recycle();
showToast(textToShow);
}
When I access the variable from MainActivity
as follows, it gives me a NullPointerException.
if (Camera2BasicFragment.newInstance().GENDER.equals("female")) {
speech = dialogs.greetings.get("female_senior_happiness");
}
I tried to use a getter method in Camera2BasicFragment
class as follows.
public Bitmap bitmap_g; // Passes the bitmap for gender classification.
public String classifyGender(Bitmap bitmap) {
return gclassifier.classifyFrame(bitmap);
}
However, the passed-in bitmap variable is also null.
I know this sounds like a common problem, but I'm a little bit clueless. Really appreciate any help. Thanks in advance!
Edit: Update codes in MainActiivty
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
if (null == savedInstanceState) {
getFragmentManager()
.beginTransaction()
.replace(R.id.container, Camera2BasicFragment.newInstance())
.commit();
}
mRobotAPI = new RobotAPI(this, robotCallback);
dialogs = new Dialogs();
//Initial speech config
speakConfig.timeout(30);
speakConfig.volume(60);
speakConfig.languageId(DialogSystem.LANGUAGE_ID_ZH_TW);
speakConfig.alwaysListenState(SpeakConfig.MODE_FOREVER);
}
//public String gender = Camera2BasicFragment.newInstance().classifyGender(Camera2BasicFragment.newInstance().bitmap_g);
//public String emotion = Camera2BasicFragment.newInstance().EMOTION;
public void sayWithExpression(){
String speech = "Hello World";
if (Camera2BasicFragment.GENDER.equals("female")) {
speech = dialogs.greetings.get("female_senior_happiness");
}
Log.d(TAG, "say /w ex " + speech);
//In debugging mode we ignore this for efficiency
//mRobotAPI.robot.setExpression(generate_expression(speech));
mRobotAPI.robot.setExpression(RobotFace.HAPPY);
mRobotAPI.robot.speak(speech, speakConfig);
}
@Override
protected void onResume() {
super.onResume();
mRobotAPI.robot.registerListenCallback(robotListenCallback);
sayWithExpression();
}
...
Camera2BasicFragment.GENDER
-> looks like you have made GENDER variable as static, you can make it non-static and should have a member variable in your MainActivity, i.e,
Camera2BasicFragment fragment = Camera2BasicFragment.newInstance();
then use this fragment
in your MainActivity's onCreate
and onResume
(i.e, in sayWithExpression
method)