I want to use Android Studio to convert a android app demo from java
to kotlin
. But I got some Errors.
some of the Java code are:
public static class FileServerAsyncTask extends AsyncTask<Void, Void, String> {
private boolean isname=true;
private String name="啦啦啦";
private Context context;
private TextView statusText;
Handler myhandler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 10:
statusText.setText("XXXXX");
}
}
};
/**
* @param context
* @param statusText
*/
public FileServerAsyncTask(Context context, View statusText) {
this.context = context;
this.statusText = (TextView) statusText;
}
The Studio convert them to
class FileServerAsyncTask
/**
* @param context
* *
* @param statusText
*/
(private val context: Context, statusText: View) : AsyncTask<Void, Void, String>() {
private val isname = true
private var name = "啦啦啦"
private var statusText: TextView
init {
this.statusText = statusText as TextView
}
internal var myhandler = object : Handler() {
override fun handleMessage(msg: Message) {
when (msg.what) {
10 -> statusText.setText("XXXXX")
else -> {
}
}
}
}
But there are errors at 10 -> statusText.setText("XXXXX")
I change it to 10 -> statusText.text = "XXXX"
, but the error still exist. However other sentences like this in the same file are ok.
What's wrong?
(private val context: Context, statusText: TextView) : AsyncTask<Void, Void, String>() {
private val isname = true
private val name = "啦啦啦"
private val statusText: TextView
internal var myhandler: Handler = object : Handler() {
override fun handleMessage(msg: Message) {
when (msg.what) {
10 -> statusText.text = "XXXXXX"
}
}
}
init {
this.statusText = statusText as TextView
}
override fun doInBackground(vararg params: Void): String? {
return null
}
}
The problem was in the constructor change view to textview.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.