从另一个 class 访问非静态变量?

Accessing non-static variables from another class?

我正在开发 Android 性别识别应用。

现在我想在 public class Camera2BasicFragment extends Fragment implements FragmentCompat.OnRequestPermissionsResultCallback 中访问保存为非静态字符串 public String GENDER 的 class化结果。我想从 MainActivity class 访问它。

我的 Camera2BasicFragment class 中有一个 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);
  }

当我按如下方式从 MainActivity 访问变量时,它给了我一个 NullPointerException。

if (Camera2BasicFragment.newInstance().GENDER.equals("female")) {
      speech = dialogs.greetings.get("female_senior_happiness");
    }

我尝试在 Camera2BasicFragment class 中使用 getter 方法,如下所示。

public Bitmap bitmap_g; // Passes the bitmap for gender classification. 
public String classifyGender(Bitmap bitmap) {
    return gclassifier.classifyFrame(bitmap);
  }

但是传入的位图变量也是null

我知道这听起来像是一个常见问题,但我有点不知所措。非常感谢任何帮助。提前致谢!

编辑:更新 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 -> 看起来你已经将 GENDER 变量设为静态,你可以将其设为非静态并且应该在你的 MainActivity 中有一个成员变量,即

Camera2BasicFragment fragment = Camera2BasicFragment.newInstance();

然后在您的 MainActivity 的 onCreateonResume 中使用此 fragment(即,在 sayWithExpression 方法中)