I'm making an Android app coding in Java just like everybody else's do. Since Android Studio 3.o Canary was released and adding support for Kotlin, I took a chance to give a try. Downloaded the plugins and setup the Gradle file correctly.
But once the activity is converted into Kotlin and synced, an error occured.
Below is my build.gradle,
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
and,
ext.kotlin_version = '1.1.3'
Dependencies, [
So, Im thinking to go back to Java until the issue is solved.
The Kotlin code are,
class Welcome : AppCompatActivity() {
internal var rujuk = FirebaseDatabase.getInstance().reference /*3rd step, DB reference*/
/*4th, initially write under onStart method, then CnP here, value inside child() should be same as in DB.*/
internal var referKpdTeksView = rujuk.child("intro")
#BindView(R.id.buku) internal var buku: ImageView? = null
#BindView(R.id.wel) internal var teksTajuk: TextView? = null /*1st step, declare variable for each Text*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_welcome)
ButterKnife.bind(this)
rujuk.keepSynced(true)
Glide.with(this).load("https://firebasestorage.googleapis.com/v0/b/travel-and-go-93552.appspot.com/o/buku.png?alt=media&token=bad59236-e4ff-44e0-81ac-32adf9c1aea4").diskCacheStrategy(DiskCacheStrategy.SOURCE).into(buku!!)
}
#OnClick(R.id.enterButton)
fun g() {
val EnterButton = Intent(this#Welcome, CountryList::class.java)
startActivity(EnterButton)
}
/*5th step, create onStart method*/
override fun onStart() {
super.onStart()
/*DB reference 4th step
* Syntax;
* DatabaseReference.addValueEventListener(new ValueEventListener)*/
referKpdTeksView.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val ayat = dataSnapshot.getValue(String::class.java)
teksTajuk!!.text = ayat
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
}
}
and the error is,
Error:Failed to delete original file 'C:\Users\MohdA\AppData\Local\Temp\gradle_download1285409691272083864bin' after copy to 'C:\Users\MohdA.gradle\caches\modules-2\files-2.1\com.android.databinding\compilerCommon\2.3.3\1f0e06d55f3f72f3192b6e026d9a5a557d9e2ea6\compilerCommon-2.3.3.jar'
In intellij i resolved by going through show history , project right click local history then show history
Related
Guys, I'm a beginner in Espresso and requesting for your valuable help, Can anyone share me the code with which I can take the screenshot for every tests which I executing ?
I'll try to guide based on the info you provided.
First you need to to know the name of your test case.
Second i will assume that you already have granted write/read external storage
Third i'll assume you also have UI Automator dependency added.
So the implementation would be something like this:
object ScreenshotUtil : TestWatcher() {
lateinit var description: Description
override fun starting(description: Description) {
super.starting(description)
this.description = description
}
fun captureScreenshot() {
val capture = Screenshot.capture()
capture.format = Bitmap.CompressFormat.PNG
capture.name = description.methodName
val processors = HashSet<ScreenCaptureProcessor>()
processors.add(BasicScreenCaptureProcessor())
try {
capture.process(processors)
} catch (e: IOException) {
throw IllegalStateException(e)
}
}
}
Then you could use it like this:
ScreenshotUtil.captureScreenshot()
I have a Kotlin class as follows (this is a sample for ease, hence it doesn't 'do' anything useful)
class MyKotlinClass (private val handleSomeCallBack: (ByteArray) -> Unit) {
private val someBuffer = BytesBuilder()
fun myFunction(bytesList: List<Byte>) {
handleSomeCallBack(someBuffer.toArray())
}
}
I want to call this code from a java class, hence, in that class I have the following declared:
public MyJavaClass() {
messageParser = new MyClass(handleSomeCallback);
}
The callback method being passed is:
private void handleSomeCallback(byte[] dataBytes) {
}
(MyClass is correctly declared within the file)
The issue I'm having is that I can't figure out how to pass the callback to the constructor of MyKotlinClass.
I have tried a variety of things including
messageParser = new MyClass(handleSomeCallback(byte[] dataBytes));
messageParser = new MyClass(this::handleSomeCallback(byte[] dataBytes));
But no matter what I try I receive an error.
I believe the answer is to do with lambdas but I can't quite see what the syntax should be for calling this from Java.
You can go with something like this:
MyKotlinClass instance = new MyKotlinClass(byteArray -> {
// your code
return Unit.INSTANCE;
});
Or use Unit as return type of your separate method like suggested by #ADM here
You just need to add
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
to your android block in app build.gradle
I need to pass intent from java activity to Kotlin activity:
Java activity ProfileActivity.class:
Intent selectGameIntent = new Intent(ProfileActivity.this, kotlin.jvm.JvmClassMappingKt.getKotlinClass(CreateNewPollUserActivity.class).getClass());
startActivity(selectGameIntent);
And this is my Kotlin activity:
class CreateNewPollUserActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_create_new_poll_user)
val max = 45
val min = 10
val total = max - min}}
When i run it i have an error:
cannot find symbol
import com.myvote.Profile.ToolbarOption.CreateNewPollUserActivity;
any ideas how send intent from java activity to Kotlin activity?
add this in build.gradle project
buildscript {
ext.kotlin_version = '1.5.21'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
And on top of build.gradle module file
apply plugin: 'kotlin-android'
This is the way to use call Kotlin class from Java class
Intent selectGameIntent = new Intent(ProfileActivity.this,
CreateNewPollUserActivity.class);
startActivity(selectGameIntent);
In your code, Probably the problem lays not in the Java-Kotlin relation, but in something else. Please check your code/project.
For this aim, you need to do several steps:
1-build.gradle project
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
2- build.gradle app
plugins {
id 'com.android.application'
id 'kotlin-android'
}
3- Introduction the class and fun:
kotlin kt:
class CountTime(val activity: Activity) {
fun seeNext(t: String, c: String, ico: Int, i: Int) {
java MainActivity
CountTime countTime = new CountTime(this);
countTime.seeNext(t,c,ico,i);
and if there is a companion object
.Companion.getInstance();
Hello so I have the problem that I registered a onSharedPreferenceChangeListener in my MainActivity. The only preference I have is a ListPreference with 3 different options. So at the start of the program it still gets triggered the first - 3 times mostly, sometimes it doesn't even trigger at the beginning. I don't think that's how it is supposed to work so my code is down below if more is needed just write a comment of a specific part.
// (from MainActivity)
this.sharedPreferences.registerOnSharedPreferenceChangeListener { sharedPreferences: SharedPreferences, s: String ->
var value = sharedPreferences.getString("location", "")
controller.setLocation(value, this)
}
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
}
Found the solution by myself, for everyone who is having an issue like that. It's probably the scope. The listener gets lost when you are switching between activities.
How to fix: put the listener block as a global variable and just register it in your "onCreate" method.
Trying to execute the below code :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Test().list.contains(1)
}
}
public class Test {
ArrayList<Integer> list;
public ArrayList<Integer> getList() {
return list;
}
}
and compilation fails at Test().list.contains(1) with message :
Task :app:compileDebugKotlin FAILED
e: /Users/sreejithcr/Documents/MyApplication/app/src/main/java/com/wxample/myapplication/MainActivity.kt: (13, 31): Overload resolution ambiguity:
public open fun contains(#Nullable element: Int!): Boolean defined in java.util.ArrayList
public open fun contains(#Nullable element: Int!): Boolean defined in java.util.ArrayList
What i understand is compiler finds 2 contains() with exact same signature and not sure which one to call.
gradle config :
ext.kotlin_version = '1.3.41'
classpath 'com.android.tools.build:gradle:3.4.2'
It is an issue with Android Studios API 29 R2
https://issuetracker.google.com/issues/139041608#comment3
Go Tools -> SDK Manager -> Uninstall Android 9.+, then install it again as Google rolled back R2 so you'll be back to R1
As I read through your code I noticed some conflicts:
First, Test needs a public constructor, which creates the ArrayList, sth. like:
public Test(){
list = new ArrayList<>();
}
Second, make the variable list private, access should only be granted through getter/setter.
Third, in method onCreate() try:
new Test().getList().contains(1);