gpt4 book ai didi

java - Android:OnClickListener 在初始加载时无响应?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:17 27 4
gpt4 key购买 nike

我遇到了一个奇怪的问题,我不确定是什么原因造成的。基本上,我使用 ErrorFragment 类来显示错误,是不是很简单?

问题与使用 setButtonClickListener 设置按钮点击监听器有关。最初加载 MainFragment 时,该按钮不会在我的 Android TV Remote 上记录任何点击。

但是,如果我使用 Remote 上的后退按钮重新加载 MainFragment,则该按钮会记录点击。

谁能告诉我为什么会发生这种情况?

谢谢!

主要 Activity .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" />

主要 fragment .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

最佳答案

你有 android API 来检查网络连接。检查此 Network Connectivity

不建议在方法 isOnline() 中访问 Process。至少在主线程中。点击监听器中的 mainFragment.isOnline() 可能是原因。尝试将其注释掉。

关于java - Android:OnClickListener 在初始加载时无响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37975409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com