-1

I'm a beginner in Android , so please pardon my ignorance. As I was learning Android programming,I followed the book and the codes are as follows:

FirstActivity.java

package com.example.activitytest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button)findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Activitytest">
        <activity
            android:name=".FirstActivity"
            android:label="This is FirstActivity"
            android:exported="true">
            <intent-filter>
                <action android:name = "android.intent.action.MAIN"/>
                <category android:name = "android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

first_layout.xml

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

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_1"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="1dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

And android studio reports that:

error: cannot find symbol
        Button button1 = (Button)findViewById(R.id.button_1);
        ^
  symbol:   class Button
  location: class FirstActivity

and then I search stackoverflow to find the method to debug. I once tried this method:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onButtonClick((Button) view);
    }
});

But it still does not work.As I am just a beginner, I can't find a way to debug. Can someone help me? Thank you for that.

赵颜哲
  • 3
  • 1

2 Answers2

1

As I can see in your code that in xml you have assigned the id to the button which is

android:id="@+id/button_1"

but you are trying to access the wrong id here

Button buttonClick = (Button)rootView.findViewById(R.id.button);

and if you are in activity there is no need of rootView you can simply access the button with findViewById(R.id.button_1)

Also it is missing in your first_layout.xml

tools:context=".FirstActivity"

add this to the constraintLayout in xml

also add import

import android.widget.Button;

And I will suggest you to use viewBinding it will help you a lot in this case

Tariq Hussain
  • 409
  • 1
  • 5
  • 19
0

add this line

import android.widget.Button;

FirstActivity.java

package com.example.activitytest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button)findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Meet Bhavsar
  • 442
  • 6
  • 12