上下文:这是javascript tutorial中的任务。该代码应生成2018-10-14 13:11:36.411 13542-13542/io.github.louistsaitszho.loft E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.louistsaitszho.loft, PID: 13542
java.lang.RuntimeException: StrictMode ThreadPolicy violation
at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1705)
at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0(StrictMode.java:1619)
at android.os.-$$Lambda$StrictMode$AndroidBlockGuardPolicy$9nBulCQKaMajrWr41SB7f7YRT1I.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1504)
at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:241)
at java.io.File.isDirectory(File.java:845)
at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:696)
at dalvik.system.DexPathList$Element.findResource(DexPathList.java:729)
at dalvik.system.DexPathList.findResources(DexPathList.java:526)
at dalvik.system.BaseDexClassLoader.findResources(BaseDexClassLoader.java:174)
at java.lang.ClassLoader.getResources(ClassLoader.java:839)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:349)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:402)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:488)
at kotlin.collections.CollectionsKt___CollectionsKt.toCollection(_Collections.kt:1062)
at kotlin.collections.CollectionsKt___CollectionsKt.toMutableList(_Collections.kt:1095)
at kotlin.collections.CollectionsKt___CollectionsKt.toList(_Collections.kt:1086)
at kotlinx.coroutines.experimental.MainDispatcherLoader.<clinit>(Dispatchers.kt:97)
at kotlinx.coroutines.experimental.Dispatchers.getMain(Dispatchers.kt:54)
at io.github.louistsaitszho.loft.ScopedFragment.getCoroutineContext(ScopedFragment.kt:20)
at kotlinx.coroutines.experimental.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:81)
at kotlinx.coroutines.experimental.BuildersKt__Builders_commonKt.launch(Builders.common.kt:49)
at kotlinx.coroutines.experimental.BuildersKt.launch(Unknown Source:1)
at kotlinx.coroutines.experimental.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:46)
at kotlinx.coroutines.experimental.BuildersKt.launch$default(Unknown Source:1)
at io.github.louistsaitszho.loft.splash.SplashFragment.onStart(SplashFragment.kt:28)
at android.support.v4.app.Fragment.performStart(Fragment.java:2372)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3206)
at android.support.v4.app.Fragment.performStart(Fragment.java:2378)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3206)
at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:206)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:605)
2018-10-14 13:11:36.412 13542-13542/io.github.louistsaitszho.loft E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
的子类ExtendedClock
,该子类允许控制台以自定义的时间间隔显示当前时间。
Clock
这是本教程中给出的解决方案中的怪异行:
Class Clock {
constructor({ template }) {
this._template = template;
}
_render() {
let date = new Date();
let hours = date.getHours();
if (hours < 10) hours = '0' + hours;
let mins = date.getMinutes();
if (mins < 10) min = '0' + mins;
let secs = date.getSeconds();
if (secs < 10) secs = '0' + secs;
let output = this._template
.replace('h', hours)
.replace('m', mins)
.replace('s', secs);
console.log(output);
}
stop() {
clearInterval(this._timer);
}
start() {
this._render();
this._timer = setInterval(() => this._render(), 1000);
}
}
} }; 另一个问题:我的代码如下。为什么这行不通?
class ExtendedClock extends Clock {
constructor(options) {
super(options);
let { precision=1000 } = options; // what is this?
this._precision = precision;
}
start() {
this._render();
this._timer = setInterval(() => this._render(), this._precision);
答案 0 :(得分:3)
这是具有默认值的对象分解语法。如果您要销毁的对象包含一个precision
键,则将使用该值,否则,将使用1000
。
如果键存在,将使用其值:
const options = { precision: 800 };
const { precision = 1000 } = options;
console.log(precision); // logs 800
但如果密钥不存在,将使用默认值:
const options = {};
const { precision = 1000 } = options;
console.log(precision); // logs 1000
您的代码可能无法正常工作,因为当您调用super.start()
时,超类会使用
setInterval(() => this._render(), 1000);
您的代码在调用此函数后会启动一个循环,但是现在这两个循环都在运行,导致渲染函数每隔1000ms被超类的setInterval
调用一次,然后又每隔precision
ms调用一次您子类的循环。
而不是在循环开始时调用super.start()
,而是尝试仅调用this._render()
。
答案 1 :(得分:1)
如果有属性,此奇怪的代码只是覆盖默认值的便捷方法。
let options = { precision: 999, template: 'h:m:s' };
let { precision = 1000 } = options; // precision === 999
let options = { template: 'h:m:s' };
let { precision = 1000 } = options; // precision === 1000
对于第二个问题,我需要更多输入。您会遇到什么错误?