【问题标题】:How to access variable defined in onCreate in onCreateView如何在 onCreateView 中访问 onCreate 中定义的变量
【发布时间】:2014-04-28 21:34:33
【问题描述】:

我正在尝试访问在 onCreateView 部分的 onCreate 部分中声明的变量“适配器”。这是一个列表。每次我检查 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。

我必须在同一个片段中使用捆绑包吗?

【问题讨论】:

  • '其他地方'? '总是'?您只需在 onCreateView() 中检查它,它在 onCreate() 之后立即调用。很久以后才会触发 onBeakonsDiscovered。
  • 为什么 onBeakonsDiscovered 的调用要晚得多?我以为它是同时调用的。此外,无论我在哪里检查,我总是从除 onBeakonsDiscovered 内部以外的任何地方得到 0。

标签: android estimote


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我应该用以下代码替换替换代码以便与 UI 一起运行吗? runOnUiThread(new Runnable() { @Override public void run() { // 注意这里报告的信标已经按照估计的 // 设备和信标之间的距离排序。adapter.replaceWith(beacons); } });
    • 或者只是在 onBeaconsDiscovered 内部运行 onCreateView 方法中的代码,或者至少为这些操作创建一个新方法并在 onBeaconsDiscovered 内部调用它。
    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多