我面临一个奇怪的问题,我不确定是什么原因造成的。基本上,我正在使用ErrorFragment
类显示错误,简单吗?
问题与使用setButtonClickListener
设置按钮单击监听器有关。最初加载MainFragment
时,该按钮不会向我的Android TV Remote 注册任何点击。
但是,如果我使用 Remote 上的“后退”按钮重新加载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();
}
}
});
}
}
最佳答案
您有android API来检查网络连接。检查此Network Connectivity
不建议在Process
方法中访问isOnline()
。在主线程中保持一致。点击监听器中的mainFragment.isOnline()
可能是原因。尝试将其注释掉。