Reputation: 78
I have the following TextView
<TextView
android:id="@+id/LoginlinkLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical center_horizontal"
android:autoSizeMaxTextSize="45sp"
android:autoSizeMinTextSize="15sp"
android:autoSizeStepGranularity="2sp"
android:text="@string/Loginlink"
android:textAlignment="center"
android:textSize="15sp"
android:onClick="loginlinkLabelonClick"
android:clickable="true"/>
And this function
fun loginlinkLabelonClick(context:Context)
{
val urlString="http://www.google.gr"
val intent = Intent(Intent.ACTION_VIEW,Uri.parse(urlString)) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.`package`="com.android.chrome"
try
{
context.startActivity(intent)
}
catch(ex:ActivityNotFoundException)
{
intent.`package`= null
context.startActivity(intent)
}
}
The error message:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.reviewer.reviewer, PID: 27809 java.lang.IllegalStateException: Could not find method loginlinkLabelonClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'LoginlinkLabel' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380) at android.view.View.performClick(View.java:6291) at android.view.View$PerformClick.run(View.java:24931) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
But as soon as I tap the textview the application crashes and I can't seem to find what's wrong. I'm new to Java, kotlin and Android studio. I had a previous experience with visual studio and c# and Java / kotlin feels a bit strange
Now I'm really trying to figure out how to open urls, but I'm having trouble getting it to work
Upvotes: 0
Views: 366
Reputation: 12005
Usually when you have a click method in the xml, the argument for the method in kotlin is a view instance, see more here. So your method should look like this:
fun loginlinkLabelonClick(view:View)
{
}
Upvotes: 4