从另一个 class 中调用 class 中的局部变量

Calling a local variable in a class from another class

我在调用这个 class

的 acce 变量时遇到了麻烦
public class Slider extends AppCompatActivity {

    ...

        public double[] acce={3.00,2.00,1.00};
        public double[] gyro={3.00,2.00,1.00};

            @Override
            protected void onCreate(Bundle savedInstanceState) {

            ...
            accelerometer = new Accelerometer(this);
        gyroscope = new Gyroscope(this);

        accelerometer.setListner(new Accelerometer.Listner() {
            @Override
            public double[] onTranslation(float ax, float ay, float az) {
                acce = setAccValue(ax,ay,az);
                return acce;
            }
...
}

从这个class

public void AntremanSayfasi(Context context){
   
    ...
            Slider slider = new Slider();
            AntremanaBasla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aa = (float) slider.acce[0];
                bb = (float) slider.acce[1];
                cc = (float) slider.acce[2];
                accx.setText(String.valueOf(aa));
                accy.setText(String.valueOf(bb));
                accz.setText(String.valueOf(cc));
            }
        });
}

问题是...我可以获得确定的值但无法获得传感器变化的更新值? 对不起,新手问题,任何帮助都会得到帮助

更新

这是主要的Class

    public class Slider extends AppCompatActivity {

    private ViewPager AktifHareket;
    private LinearLayout mDotLayout;
    private String asd;
    private int asdf;
    private Antreman antreman;
    TextView DenemeSayiCek;
    int position;
    View view;
    int count;
    View viewFix;
    Context context;
//Sensors-------------------------------------------------------------------------------------------
    private Accelerometer accelerometer;
    private Gyroscope gyroscope;
    
    public double[] acc={3.00,2.00,1.00};
    public double[] gyr={3.00,2.00,1.00};
    public double[] acce={5.00,5.00,5.00};
    public double[] gyro={3.00,2.00,1.00};
    //Sensors---------------------------------------------------------------------------------------

    //Main------------------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Antreman antreman = new Antreman();
        context = this;
        setContentView(R.layout.activity_slider);
        Login login = new Login();
        AktifHareket = (ViewPager) findViewById(R.id.slideViewPager);
        mDotLayout = (LinearLayout) findViewById(R.id.dotsLayout);
        SliderAdapter sliderAdapter = new SliderAdapter(this);
        AktifHareket.setAdapter((sliderAdapter));

        addDotsIndicator();

        //Saydir Buton------------------------------------------------------------------------------
        TextView Deneme = (TextView) findViewById(R.id.deneme);
        TextView Deneme4 = (TextView) findViewById(R.id.deneme4);
        TextView Deneme3 = (TextView) findViewById(R.id.deneme3);
        TextView Deneme2 = (TextView) findViewById(R.id.deneme2);
        Button Saydir = (Button) findViewById(R.id.SaydirSlide);
        Button Saydir2 = (Button) findViewById(R.id.Saydir2Slide);
        Saydir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                position = AktifHareket.getCurrentItem();
                view = AktifHareket.getChildAt(position);
                count = Integer.valueOf(AktifHareket.indexOfChild(view));
                viewFix = AktifHareket.getChildAt(count);
                DenemeSayiCek = (TextView) viewFix.findViewById(R.id.TekrarSayisiSlide);
                asd = (String) DenemeSayiCek.getText().toString();
                asdf = Integer.valueOf(asd);
                asdf++;
                DenemeSayiCek.setText(String.valueOf(asdf));
                Deneme.setText(Arrays.toString(acce));
                Deneme2.setText(String.valueOf(position));
                Deneme3.setText(Arrays.toString(acce));
                Deneme4.setText(String.valueOf(SliderAdapter.POSITION_NONE));

            }
        });

        //Saydir Buton------------------------------------------------------------------------------
        //ImageButton-------------------------------------------------------------------------------
        ImageView logoImage =  (ImageView) findViewById(R.id.logo);
        logoImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antreman.AntremanSayfasi(context);
            }
        });
        antreman.AntremanSayfasi(context);//Açılışta Sayfayı açsın diye
        //ImageButton-------------------------------------------------------------------------------
        //Sensors-----------------------------------------------------------------------------------
        accelerometer = new Accelerometer(this);
        gyroscope = new Gyroscope(this);

        accelerometer.setListner(new Accelerometer.Listner() {
            @Override
            public double[] onTranslation(float ax, float ay, float az) {
                acce = setAccValue(ax,ay,az);
                Antreman.SensorValues.accValues=acce;
                return acce;
            }

        });

        gyroscope.setListner(new Gyroscope.Listner() {
            @Override
            public void onRotation(float gx, float gy, float gz) {
                setGyroValue(gx,gy,gz);
            }
        });

        //Sensors-----------------------------------------------------------------------------------
    }
    //Sensors---------------------------------------------------------------------------------------
    protected void onResume(){
        super.onResume();

        accelerometer.register();
        gyroscope.register();

    }
    protected void onPouse(){
        super.onPause();

        accelerometer.unregister();
        gyroscope.unregister();

    }

    public double[] setAccValue(double ac, double bc, double cc){
        this.acc[0] = ac;
        this.acc[1] = bc;
        this.acc[2] = cc;
        return acc;
    }

    public double[] setGyroValue(float qq, float wq, float eq){
        this.gyr[0] = qq;
        this.gyr[1] = wq;
        this.gyr[2] = eq;
        return gyr;
    }

    public void getAccValue(double ac, double bc, double cc){
        ac = (float) acce[0];
        bc = (float) acce[1];
        cc = (float) acce[2];

    }
    public void getGyroValue(float qq, float wq, float eq){

        qq = (float) gyro[0];
        wq = (float) gyro[1];
        eq = (float) gyro[2];

    }

    //Sensors---------------------------------------------------------------------------------------
    public void addDotsIndicator(){

        TextView[] mDots = new TextView[3];

        for(int i = 0; i < mDots.length; i++){
            mDots[i] = new TextView(this);
            mDots[i].setText(Html.fromHtml("&#8226;"));
            mDots[i].setTextSize(35);
            mDots[i].setTextColor(getResources().getColor(R.color.colorPrimaryDark));

            mDotLayout.addView(mDots[i]);
        }

    }

    //----------------------------------------------------------------------------------------------

}

ant 这是 class 我需要从主 class

初始化这些值
public class Antreman {
    private Accelerometer accelerometer;
    private Gyroscope gyroscope;
    public double aa;
    public float bb;
    public float cc;
    public float qq;
    public float ww;
    public float ee;

    //Pop up----------------------------------------------------------------------------------------
        public void AntremanSayfasi(Context context){
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        final View popup_durum = li.inflate(R.layout.popup_durum,null);
        Button AntremanaBasla = popup_durum.findViewById(R.id.basla);
        Button Cikis = popup_durum.findViewById(R.id.cikis);

        dialogBuilder.setView(popup_durum);
        AlertDialog dialog = dialogBuilder.create();
        dialog.show();

            TextView gyrox = (TextView) popup_durum.findViewById(R.id.gyrox);

        AntremanaBasla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aa = SensorValues.accValues[0];
                gyrox.setText(String.valueOf(aa));

            }

        });
        Cikis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    static class SensorValues{
        public static double[] accValues = {1.00};
    }
}

你可以在这种情况下使用common class的概念,我相信它会很容易地解决你的问题。按照以下步骤操作。

制作一个public class并在其中声明一个double数组,你需要在另一个class[=14中访问它=]

  Public class Common{
         public static List<double> acce;
    }

并且在您的 activity 中传感器提供更新值的地方使用临时列表 并将其分配给 Common.acce;

Common.acce = tempList;

现在,当您需要访问 acce 时,只需使用 Common.acce,tempList 的引用在 Common.acce 中,当您的传感器更新 tempList 中的数据时common 列表也会更新,所以最终你会得到更新后的值。

如有不明之处欢迎提问。如果对您有帮助,请标记为正确答案,以便日后此答案能帮助到其他有需要的人。快乐编码 .