如何在onCreateView中访问onCreate中定义的变量

时间:2014-04-28 21:34:33

标签: android estimote

我正在尝试访问onCreateView部分的onCreate部分中声明的变量“adapter”。这是一个清单。每当我检查onCreate之外的内容时,它都是空的。

这是代码

package com.example.beacon;

import java.util.List;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;

import android.app.Fragment;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentBeacon extends Fragment {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
    private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId",
            ESTIMOTE_PROXIMITY_UUID, null, null);

    private BeaconManager beaconManager;
    public LeDeviceListAdapter adapter = null; //<---- this variable

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new LeDeviceListAdapter(getActivity());
        beaconManager = new BeaconManager(getActivity());

        beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
                Log.d(TAG, "Ranged beacons FB_onCreate: " + beacons);
                adapter.replaceWith(beacons); //<---- this variable
                Log.d(TAG, "adapters found: " + adapter.getCount());
            }
        });
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });

        Log.d(TAG, "Adapters found2: " + adapter.getCount()); //<---- this variable
        View rootView = inflater.inflate(R.layout.fragment_beacon, container,
                false);

        TextView[] pairs = new TextView[adapter.getCount()]; //<---- this variable
        for (int l = 0; l < adapter.getCount(); l++) {
            pairs[l] = new TextView(getActivity());
            pairs[l].setTextSize(15);

            pairs[l].setId(l);
            pairs[l].setText((l + 1) + ": something");
            ((ViewGroup) rootView).addView(pairs[l]);
        }

        return rootView;
    }
    @Override
    public void onStop() {

        // Should be invoked in #onStop.
        try {
            beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
        } catch (RemoteException e) {
            Log.e(TAG, "Cannot stop but it does not matter now", e);
        }
        super.onStop();
    }

    @Override
    public void onDestroy() {

        // When no longer needed. Should be invoked in #onDestroy.
        beaconManager.disconnect();
        super.onDestroy();
    }

}

当我从onBeaconsDiscovered运行它时,计数是!= 0这是正确的。但在任何其他地方它总是0。

我是否必须在同一片段内使用捆绑包?

2 个答案:

答案 0 :(得分:2)

正如Sim所说:onBeaconsDiscovered正在异步运行。确切地说,默认情况下每秒调用一次(根据Estimote SDK文档)。

在onCreateView中,这个onBeaconsDiscovered尚未被调用,这就是为什么你看到count == 0.你需要从onBeaconsDiscovered方法更新UI(或构造UI元素)。

答案 1 :(得分:0)

onBeaconsDiscovered方法将在调用onCreateView之后异步运行。