我正在开发一个用于性别识别的 Android 应用程序。
现在我想访问保存为非静态字符串的分类结果public String GENDER
,在public class Camera2BasicFragment extends Fragment implements FragmentCompat.OnRequestPermissionsResultCallback
. 我想从MainActivity
课堂上访问它。
我Camera2BasicFragment
班上有一个分类方法。
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);
}
MainActivity
当我从如下访问变量时,它给了我一个 NullPointerException。
if (Camera2BasicFragment.newInstance().GENDER.equals("female")) {
speech = dialogs.greetings.get("female_senior_happiness");
}
我尝试在Camera2BasicFragment
类中使用 getter 方法,如下所示。
public Bitmap bitmap_g; // Passes the bitmap for gender classification.
public String classifyGender(Bitmap bitmap) {
return gclassifier.classifyFrame(bitmap);
}
但是,传入的位图变量也是空的。
我知道这听起来像是一个常见的问题,但我有点无能为力。非常感谢任何帮助。提前致谢!
编辑:更新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();
}
...