Android按id查找返回旧视图
我尝试在Android中实现一个计时器,虽然它计时,但并没有完全按照预期工作 当计时器过期时,我有以下代码(位于它自己的方法中): 按钮是一个简单地显示广告的按钮,用户可以获得奖励。简单对吧 当广告在后台加载完毕时,该按钮将启用,以便在用户单击时为其准备好内容 计时器将再次检查ad是否已加载。如果广告失败了,我会知道,因为按钮仍将被禁用,或者文本仍将被加载 如果该按钮已启用,我希望计时器完成其进程并自由 我想这很简单。创建一个好的计时器。检查按钮,如果需要,重新加载广告。完成 但问题是返回的按钮仍然显示文本正在加载,并且已被禁用。我知道按钮不是真的,虽然我可以看到屏幕上的按钮既没有被禁用,也没有加载它的文本。那么,为什么按id查找会返回一个禁用的按钮呢?这就像是在更换按钮之前,它返回了一个旧版本的按钮 这也会导致无限循环,因为返回的按钮状态与屏幕按钮状态从不匹配 那么,我怎样才能让计时器按我期望的方式工作呢?为什么它会返回旧按钮 下面是一个完整的例子来说明这个问题 视图:Android按id查找返回旧视图,android,timer,Android,Timer,我尝试在Android中实现一个计时器,虽然它计时,但并没有完全按照预期工作 当计时器过期时,我有以下代码(位于它自己的方法中): 按钮是一个简单地显示广告的按钮,用户可以获得奖励。简单对吧 当广告在后台加载完毕时,该按钮将启用,以便在用户单击时为其准备好内容 计时器将再次检查ad是否已加载。如果广告失败了,我会知道,因为按钮仍将被禁用,或者文本仍将被加载 如果该按钮已启用,我希望计时器完成其进程并自由 我想这很简单。创建一个好的计时器。检查按钮,如果需要,重新加载广告。完成 但问题是返回的按钮
我建议你提供一份工作。特别是,显示您在何处以及如何执行上述代码,包括如何设置和使用计时器。使用上面的方法将在timersdone方法中生成一个无限循环,我还注意到计时器没有取消。即使调用cancel方法,计时器也会一直运行到完成。很明显,如果启用成功,我希望计时器停止。但是启用过程很顺利,文本也会改变,但计时器不会取消。我建议您提供一个。特别是,显示您在何处以及如何执行上述代码,包括如何设置和使用计时器。使用上面的方法将在timersdone方法中生成一个无限循环,我还注意到计时器没有取消。即使调用cancel方法,计时器也会一直运行到完成。很明显,如果启用成功,我希望计时器停止。但是启用过程很顺利,文本也会改变,但计时器不会取消。
Button b = (Button) this.findViewById(R.id.actionButton);
if(b != null && (!b.isEnabled() || b.getText().toString() == "Loading.."))
{
Toast.makeText(this, "Ad failed. Loading next ad...", Toast.LENGTH_SHORT).show();
SetupNextAdd(true); // The ad failed
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="350dp"
android:background="@android:color/holo_blue_bright"
android:onClick="ShowAdd"
android:padding="5dp"
android:text="Loading..."
android:enabled="false"/>
<RelativeLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private RewardedVideoAd mAd;
private AdView mAdView;
private CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new CountDownTimer(15000, 5000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
TimerDone();
}
};
SetupNextAdd();
ShowBannerAd();
}
public void TimerDone()
{
Button b = (Button) findViewById(R.id.actionButton);
if(b != null && (!b.isEnabled() || b.getText().toString() == "Loading.."))
{
Toast.makeText(this, "Ad failed. Loading next ad...", Toast.LENGTH_SHORT).show();
SetupNextAdd(); // The ad failed
}
}
public void EnableDisableButton(String action)
{
MainActivity a = this;
Button b = (Button)a.findViewById(R.id.actionButton);
if(action == "enable")
{
b.setEnabled(true);
b.setText("View Ad");
timer.cancel();
}
else
{
b.setEnabled(false);
b.setText("Loading..");
}
}
public void ShowAdd(View view)
{
if(mAd.isLoaded()) {
mAd.show();
}
}
private void SetupNextAdd()
{
timer.cancel();
timer.start();
if(mAd == null) {
buildReward();
}
requestNewReward();
}
private void buildReward()
{
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
EnableDisableButton("enable");
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
EnableDisableButton("disable"); // disable till loaded. No reward for you
}
@Override
public void onRewarded(RewardItem rewardItem) {
EnableDisableButton("disable");
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Toast.makeText(getApplicationContext(), "ad failed. Try again.", Toast.LENGTH_SHORT).show();
EnableDisableButton("disable");
SetupNextAdd();
}
});
}
private void requestNewReward()
{
mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
private void ShowBannerAd()
{
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}