Android:OnClickListener在初始加载时没有响应?

时间:2016-06-22 18:11:48

标签: java android android-fragments android-tv

我面临一个奇怪的问题,我不确定是什么导致了它。基本上,我使用ErrorFragment类来显示错误,简单吧?

问题与使用setButtonClickListener设置按钮单击侦听器有关。最初加载MainFragment时,该按钮不会记录我的Android TV遥控器的任何点击次数。

但是,如果我使用遥控器上的后退按钮重新加载MainFragment,则按钮会记录点击次数。

任何人都可以告诉我为什么会发生这种情况吗?

谢谢!

MainActivity.java:

public class MainActivity extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml中:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_browse_fragment"
    android:name="com.ui.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ui.MainActivity"
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" />

MainFragment.java:

public class MainFragment extends DetailsFragment
{
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        setupUIElements();

        if (isConnectingToInternet() == true)
        {
            prepareBackgroundManager();
            loadRows();
            setupEventListeners();
        }
        else
        {
            InternetConnectivityFragment internetError = new InternetConnectivityFragment();

            getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, internetError).commit();
        }
    }

    public class GetInternetStatus extends AsyncTask<Void,Void,Boolean>
    {

        @Override
        protected Boolean doInBackground(Void... params)
        {

            return hasInternetAccess();
        }

        protected boolean hasInternetAccess()
        {
            try
            {
                URL url = new URL("http://www.google.com");

                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                urlc.setRequestProperty("User-Agent", "Android Application:1");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(1000 * 30);
                urlc.connect();

                // http://www.w3.org/Protocols/HTTP/HTRESP.html
                if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
                {
                    // Requested site is available
                    return true;
                }
            }
            catch (Exception ex)
            {
                // Error while trying to connect
                ex.printStackTrace();
                return false;
            }
            return false;
        }
    }

    // Checking for all possible internet connections
    public  boolean isConnectingToInternet()
    {
        Boolean result = false;

        try
        {
            //get the result after executing AsyncTask
            result = new GetInternetStatus().execute().get();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        return result;
    }

    ....
}

InternetConnectivityFragment.java:

public class InternetConnectivityFragment extends android.support.v17.leanback.app.ErrorFragment
{
    private static int TIMER_DELAY = 3000;
    private static final String TAG = "InternetFragment";
    private static final boolean TRANSLUCENT = true;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setErrorContent();
    }

    private void setErrorContent()
    {
        setImageDrawable(getResources().getDrawable(R.drawable.lb_ic_sad_cloud, null));
        setMessage(getResources().getString(R.string.no_internet_message));
        setDefaultBackground(TRANSLUCENT);

        setButtonText(getResources().getString(R.string.retry_connection));

        setButtonClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                MainFragment mainFragment = new MainFragment();

                if (mainFragment.isOnline() == true)
                {
                    getFragmentManager().beginTransaction().remove(InternetConnectivityFragment.this).commit();
                    getFragmentManager().popBackStack();
                }
            }
        });
    }

}

enter image description here

1 个答案:

答案 0 :(得分:5)

您有Android API来检查网络连接。选中Network Connectivity

不建议访问方法Process中的isOnline()。至少在主线程中。点击监听器中的mainFragment.isOnline()可能是原因。试着评论一下。