How to handle click from xml to Activity

我的未来我决定 提交于 2020-03-25 19:01:25

问题


I am new to android and following this tutorial: https://www.youtube.com/watch?v=qK0QNA0sMGc&t=2710s (It's in Hindi)

In my MainActivity.java:


When I'm trying to run the app it's giving errors saying:

9 errors found
1 warning found
2 typos found

It's asking me to put a semicolon at the end of
Log.i(tag:"this", msg:"clickbtn: This is a message");
which I have already put down.

This is my activity_main.xml:

Can anybody tell me what are the errors?

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/topText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:includeFontPadding="false"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:text="Welcome to Shivam Travels"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.504"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:hint="Enter your username"
    android:inputType="textPersonName"
    app:layout_constraintStart_toStartOf="@+id/topText"
    app:layout_constraintTop_toBottomOf="@+id/topText" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:hint="Enter your password"
    android:inputType="textPassword"
    app:layout_constraintStart_toStartOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:onClick="clickbtn"
    android:text="Sign In"
    app:layout_constraintStart_toStartOf="@+id/checkBox"
    app:layout_constraintTop_toBottomOf="@+id/checkBox" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="176dp"
    android:layout_height="65dp"
    android:layout_marginTop="32dp"
    android:text="Remember me!"
    app:layout_constraintStart_toStartOf="@+id/editText2"
    app:layout_constraintTop_toBottomOf="@+id/editText2" />


回答1:


You took the code from a screenshot. The problem is that Android studio added information that shows the name of the parameter when you call a method using literals.

This information is not part of the code and Android Studio just adds it to inform you about the parameter and make the code more readable.

The actual code should be:

Log.i("this", "clickbtn: this is a message");

instead of:

Log.i(tag:"this", msg:"clickbtn: this is a message");

If you use this, Android Studio will show you the tag: and message: even if it is not part of your code.




回答2:


You define above type not supported in java.You can do like this.

public String tag = "this";
public String msg = "clickbtn:this is a message!";


Log.i(tag,msg);



回答3:


Remove tag: and msg: from your code




回答4:


Replace

Log.i(tag:"this", msg:"clickbtn: This is a message");

with

Log.i("this","clickbtn:this is a message!");



回答5:


Replace you code with this code

Replace

Log.i(tag:"this", msg:"clickbtn: This is a message");

with

Log.i("This","This is a message");

Below full snippet of you code

JAVA FILE

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainTestActivity extends Activity {
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
    }

    public void clickbtn(View view){
        Log.i("This","This is a message");
        Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
    }
}

XML FILE

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/topText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:includeFontPadding="false"
        android:paddingTop="10dip"
        android:paddingBottom="10dip"
        android:text="Welcome to Shivam Travels"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="Enter your username"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="@+id/topText"
        app:layout_constraintTop_toBottomOf="@+id/topText" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="Enter your password"
        android:inputType="textPassword"
        app:layout_constraintStart_toStartOf="@+id/editText"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:onClick="clickbtn"
        android:text="Sign In"
        app:layout_constraintStart_toStartOf="@+id/checkBox"
        app:layout_constraintTop_toBottomOf="@+id/checkBox" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="176dp"
        android:layout_height="65dp"
        android:layout_marginTop="32dp"
        android:text="Remember me!"
        app:layout_constraintStart_toStartOf="@+id/editText2"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>

For more details : solution

Hope this may helps you now.



来源:https://stackoverflow.com/questions/59906920/how-to-handle-click-from-xml-to-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!