Tag: android

将MainActivity转换为Kotlin之后的后续构建中的ClassNotFoundException

我想给Kotlin一个尝试,所以我决定尝试将我的项目的单个类转换为Kotlin,并在尝试转换整个项目之前查看它如何与我的工作流程集成。 我能够构建和我的应用程序正常工作,但在第一个后,每一个后续的构建,我从ClassLoader下面的异常。 该应用程序只能清理后正常工作,并在我转换任何东西之前工作正常。 我可以在同一台机器上多次运行HelloWorld项目,而不会出错。 在这个问题中的回应没有帮助: 在使用kotlin的自定义风格的ClassNotFoundException 07-31 14:35:41.563 1878-1878/com.ddiehl.android.htn.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.ddiehl.android.htn.debug, PID: 1878 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ddiehl.android.htn.debug/com.ddiehl.android.htn.view.activities.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.ddiehl.android.htn.view.activities.MainActivity" on path: DexPathList[[zip file "/data/app/com.ddiehl.android.htn.debug-1/base.apk"],nativeLibraryDirectories=[/data/app/com.ddiehl.android.htn.debug-1/lib/arm, /vendor/lib, /system/lib]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at […]

如何使用Kotlin异步并等待阶乘?

我在Kotlin(Android应用程序)中有这个功能: tailrec fun factorial(n: BigInteger, remainder: BigInteger = BigInteger.ONE) : BigInteger{ if(n== BigInteger.ZERO) return remainder else { return factorial(n – BigInteger.ONE, remainder * n) } } 和这个简单的代码: button.setOnClickListener { val n = editTextT.text.toString() val result: BigInteger = factorial(BigInteger(n)) textView.text = "$n! is $result" } 我的问题是:有没有办法以异步的方式做这种计算? 如果是的话,怎么样?

Kotlin使用Parcelable将ListArray传递给另一个Activity

我有一个自定义类充满了用户的信息。 我使用ArrayList来保存所有的用户类数据。 我想将数组传递给可以读取和修改的另一个活动。 由于数组可能很大,因此我需要尽可能高效和快速地完成此过程,这就是为什么我选择使用Parcelable而不是Serializable。 我在使用Kotlin时遇到了麻烦。 有很多的Java教程,但没有在Kotlin,这是什么使这个问题不同,那么所有其他人。 有人可以提供和解释Kotlin的代码如何实现? 这是我迄今为止: constructor(`in`:Parcel) { CollegeName = `in`.readString() } override fun describeContents(): Int { return 0 } override fun writeToParcel(dest: Parcel?, flags: Int) { } private fun readFromParcel(`in`:Parcel) { CollegeName = `in`.readString() } companion object { @JvmField final val CREATOR: Parcelable.Creator<College> = object: Parcelable.Creator<College> { override fun createFromParcel(source: Parcel): College […]

Kotlin:在命令行中运行kapt来为DI生成存根

我试图从命令行运行kapt3为Kotlin文件生成Java存根。 语法似乎是: kotlinc -P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubs -classpath $CLASSPATH:$ANDROID_HOME/platforms/android-23/android.jar -d $OUT $SRCS 它是否正确? 我没有看到kotlin/lib文件夹中的插件。 目前我正试图从源代码构建它,但下载它的“官方”方式是什么? 补充笔记: 这是一个Android项目,因此包含了android.jar 我的用例:该项目正在编译与BUCK 。 不是gradle 。 Buck现在还不支持kapt ,而且我需要为DI(依赖注入)生成存根,以在kotlin代码上正常工作。

Android SDK类上的Kotlin扩展函数的单元测试

Kotlin扩展功能非常棒。 但是我怎么能对它们进行单元测试呢? 尤其是那些Android SDK提供的类(如Context,Dialog)。 我在下面提供了两个例子,如果任何人都可以分享我可以如何进行单元测试,或者如果我真的想单元测试它们,我需要以不同的方式编写它们。 fun Context.getColorById(colorId: Int): Int { if (Build.VERSION.SDK_INT >= 23) return ContextCompat.getColor(this, colorId) else return resources.getColor(colorId) } 和 fun Dialog.setupErrorDialog(body : String, onOkFunc: () -> Unit = {}): Dialog { window.requestFeature(Window.FEATURE_NO_TITLE) this.setContentView(R.layout.dialog_error_layout) (findViewById(R.id.txt_body) as TextView).text = body (findViewById(R.id.txt_header) as TextView).text = context.getString(R.string.dialog_title_error) (findViewById(R.id.txt_okay)).setOnClickListener{ onOkFunc() dismiss() } return this } 任何建议将有所帮助。 谢谢!

错误':android:transformKotlinClassesWithJillForDebug'。 构建用Kotlin + LibGDX编写的Android应用程序时

我正在使用LibGDX框架在Kotlin上开发游戏。 我在构建Android应用程序时遇到问题。 桌面和IOS( moe框架 )建立好。 这是完整的信息: Error:A problem was found with the configuration of task ':android:transformKotlinClassesWithJillForDebug'. > File '/Users/maximternovtsi/bagel/android/build/tmp/kotlin-classes/debug.jar' specified for property 'inputJarFile' does not exist. 我的build.gradle文件: buildscript { repositories { mavenLocal() mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'org.multi-os-engine:moe-gradle:1.3.6' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.1" } } allprojects { apply plugin: "eclipse" […]

如何在Kotlin中编写下面的代码来实现回调

我如何在Java中写入Kotlin? Callback callback= new Callback() { @Override public void getCallback(ServerResponse serverResponse) { } }

Kotlin Android扩展和保留片段

我在项目中使用Kotlin Android扩展 ,遇到了一些我无法理解的行为。 我使用这段代码来保留我的片段: val fragment = fragmentManager.findFragmentByTag("hello") ?: HelloFragment() fragmentManager.beginTransaction() .replace(R.id.fragment_container, fragment, "hello") .commit() 这是保留的Fragment : import kotlinx.android.synthetic.hello.* public class HelloFragment : Fragment() { val text = "Hello world!" override fun onCreate(savedInstanceState: Bundle?) { super<Fragment>.onCreate(savedInstanceState) setRetainInstance(true) } override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater?.inflate(R.layout.hello, container, false) } override fun […]

使用另一个文件Kotlin的变量

如果我有2个活动,我想添加如果从另一个活动的变量条件我怎么能做到这一点? 就像如果我有第一个布局(第一个活动)中包含9个数字的变量,我想设置如果在另一个使用x变量是问题的条件。 我正在用kotlin使用Android Studio。

Android Kotlin:使用Mockito / PowerMock嘲笑免费的功能

我在Kotlin有一个免费的功能,我想模拟测试目的。 我的免费功能是这样的: fun somethingAwesomeIsDoneHere(param: String) : String { // Awesome } 在我的测试中,我想模拟/存根,以便当它被称为模拟值返回。 when(somethingAwesomeIsDoneHere(something)).thenReturn(retValue) 即使PowerMock可以完成,请让我知道。 PS:我正在为我的项目使用Kotlin 1.0.1-2。