收到第一个结果后,如何删除观察者?下面是我尝试过的两种代码方式,但即使我已经删除了观察者,它们也都会继续接收更新.
Observer observer = new Observer<DownloadItem>() {
@Override
public void onChanged(@Nullable DownloadItem downloadItem) {
if(downloadItem!= null) {
DownloadManager.this.downloadManagerListener.onDownloadManagerFailed(null, "this item already exists");
return;
}
startDownload();
model.getDownloadByContentId(contentId).removeObservers((AppCompatActivity)context);
}
};
model.getDownloadByContentId(contentId).observeForever(observer);
Run Code Online (Sandbox Code Playgroud)
model.getDownloadByContentId(contentId).observe((AppCompatActivity)context, downloadItem-> {
if(downloadItem!= null) {
this.downloadManagerListener.onDownloadManagerFailed(null, "this item already exists");
return;
}
startDownload();
model.getDownloadByContentId(contentId).removeObserver(downloadItem-> {});
} );
Run Code Online (Sandbox Code Playgroud) 当我插入 fontSize =DimensionResource(id = R.dimen.textLabelTextSize) 时,其中 dimens 或 54sp 或 60sp 取决于设备,我在 Text() 上收到错误“无法使用提供的参数调用以下函数。 ” 但是当我输入像 54sp 这样的硬编码值时就很好了。奇怪的是填充修饰符维度资源(以 dp 为单位)工作正常。
Text(
text = textLabelItem.textLabel,
modifier = Modifier
.padding(
start = dimensionResource(id = R.dimen.textLabelPaddingVertical),
top = dimensionResource(id = R.dimen.textLabelPaddingHorizontalTop),
end = dimensionResource(id = R.dimen.textLabelPaddingVertical),
bottom = dimensionResource(id = R.dimen.textLabelPaddingHorizontalBottom)
)
.background(colorResource(id = R.color.textLabelBg))
.border(
width = 2.dp,
color = colorResource(id = R.color.textLabelBorder),
shape = RoundedCornerShape(8.dp)
),
color = colorResource(id = android.R.color.background_dark),
fontSize = dimensionResource(id = R.dimen.textLabelTextSize),
fontWeight = FontWeight.Bold
)
Run Code Online (Sandbox Code Playgroud) 从 v5.6.0 开始,不推荐使用 adView.setAdListener。我们现在如何接收 onAdLoaded 和 onError 回调?
我以前只安装了稳定版的AS(AS 4.0),偶尔启动不了。它会显示它的加载屏幕,但就是这样。如果我将鼠标悬停在状态栏中的 AS 图标上,它会显示一个空白(白色)屏幕。然而,它会显示“提示”窗口。我不得不卸载/重新安装它,有时需要多次尝试才能成功。这可以解决问题,但不是解决方案。
现在我并排安装了预览测试版(AS 4.1-beta4),正是那个版本偶尔会出现问题,而稳定版有趣地变得稳定了。
我的 JAVA_HOME 环境变量为:C:\Program Files\Android\android-studio\jre\bin 并添加了第二个,以防万一:C:\Program Files\Android\android-studio-preview\android-工作室\jre\bin
我也有一个 JDK_HOME 变量,但在我阅读它后删除它是多余的。它没有改变任何东西。我使用的是 Windows 10
更新:我现在注意到此问题仅在特定项目由于计算机电池耗尽或 Windows 崩溃而未正确关闭时发生。我是否应该删除其中一个文件夹或文件以“重置”项目以进行干净的 Android Studio 构建?
我不断收到以下错误:
无法弄清楚如何将该字段保存到数据库中。您可以考虑为其添加类型转换器。
我在数据库类上将@TypeConverter添加到一对转换器方法和@TypeConverters批注中。
DownloadItem.java
@Entity(tableName = "downloads")
public class DownloadItem implements Serializable {
public enum DownloadStatus {
None(0),
Downloading(1),
Suspended(2),
Completed(3),
Deleted(4),
Expired(5);
private final int numeral;
@TypeConverter
public static DownloadStatus getDownloadStatus(int numeral){
for(DownloadStatus ds : values()){
if(ds.numeral == numeral){
return ds;
}
}
return null;
}
@TypeConverter
public static Integer getDownloadStatusInt(DownloadStatus status){
return status.numeral;
}
DownloadStatus(int num){
this.numeral = num;
}
}
@TypeConverters(DownloadStatus.class)
@ColumnInfo(name = "download_status")
public DownloadStatus downloadStatus;
@PrimaryKey
@NonNull
private int contentId;
@Ignore
public DownloadItem() {
} …Run Code Online (Sandbox Code Playgroud) 我正在使用JobScheduler.setPeriodic()来重复我的JobIntentService.它第一次工作,但从不重复.在Android 7.0上运行
ConcurrentCheck.java
int mdelaymilles = 30000;
JobScheduler jobScheduler = (JobScheduler) mActivity.getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName componentName = new ComponentName(mActivity, ConcurrentCheckService.class);
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, componentName)
.setPeriodic(mdelaymilles)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build();
int resultCode = jobScheduler.schedule(jobInfo);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(LOG_TAG, "Job scheduled!");
Intent intent = new Intent();
intent.putExtra(LAST_POSITION, lastPosition);
JobIntentService.enqueueWork(mActivity, ConcurrentCheckService.class, JOB_ID, intent);
} else {
Log.d(LOG_TAG, "Job not scheduled");
}
Run Code Online (Sandbox Code Playgroud) 使用 Firebase Crashlytics SDK,文档解释了如何 强制崩溃
但是,在该行中:
Crashlytics.getInstance().crash();
Run Code Online (Sandbox Code Playgroud)
Crashlytics 无法识别且无法导入。
我尝试切换到 FirebaseCrashlytics.getInstance() 但没有 crash() 方法。
注意:Crashlytics 确实设置正确 - 我可以通过查找一个不存在的视图并正确报告它来导致我自己的崩溃。
我想在单击箭头图像后禁用它,并可以选择在另一个事件后再次启用它。
Image(
painterResource(R.drawable.arrow_back_btn),
contentDescription = "back",
modifier = Modifier
.padding(start = 16.dp, top = 16.dp)
.clickable(onClick = onArrowClick)
.layoutId(R.id.arrowBackBtn),
onArrowClick = {enabled = false}
)
Run Code Online (Sandbox Code Playgroud)
我只是即兴创作的最后一行 - onArrowClick 被识别,但“启用”未被识别
我将 ViewPager2 与 TabLayout 连接。其他提到 ViewPager 的帖子提供了重写 setCurrentItem(position, false) 的方法,其中 false 会禁用平滑滚动。但是,TabLayoutMediator 调用 onTabSelected(TabLayout.Tab tab) ,后者调用 viewpager.setCurrentItem(postion, true)。如果 TabLayoutMediator 以及 ViewPager2 都是最终的,我如何覆盖此行为?
已经尝试过:
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position, false);
}
});
Run Code Online (Sandbox Code Playgroud)
并像这样重写 onTabSelected :
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(viewPager.getPosition(), false);
}
......
}
Run Code Online (Sandbox Code Playgroud) android ×9
android-room ×2
ads ×1
crashlytics ×1
enums ×1
firebase ×1
kotlin ×1
observers ×1
onclick ×1
orm ×1
windows-10 ×1