首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"Button button1 = (Button)findViewById(R.id.button_1)“在Android中运行

"Button button1 = (Button)findViewById(R.id.button_1)“在Android中运行
EN

Stack Overflow用户
提问于 2022-04-24 09:20:37
回答 2查看 251关注 0票数 -1

我是Android的初学者,所以请原谅我的无知。当我学习Android编程时,我遵循了这本书,代码如下:

FirstActivity.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

据安卓工作室报道:

代码语言:javascript
复制
error: cannot find symbol
        Button button1 = (Button)findViewById(R.id.button_1);
        ^
  symbol:   class Button
  location: class FirstActivity

然后搜索堆栈溢出以找到要调试的方法。我曾经尝试过这样的方法:

代码语言:javascript
复制
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);
    }
});

但是它仍然不work.As --我只是个初学者,我找不到调试的方法。有人能帮我吗?谢谢你这么做。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-24 09:29:50

正如我在您的代码中所看到的,在xml中,您已经将id分配给按钮,该按钮是

代码语言:javascript
复制
android:id="@+id/button_1"

但是你想在这里访问错误的身份

代码语言:javascript
复制
Button buttonClick = (Button)rootView.findViewById(R.id.button);

如果您正在活动,则不需要rootView,只需使用findViewById(R.id.button_1)访问按钮即可。

此外,在您的first_layout.xml中也缺少它。

代码语言:javascript
复制
tools:context=".FirstActivity"

将其添加到xml中的constraintLayout

还添加了导入

代码语言:javascript
复制
import android.widget.Button;

我建议你使用viewBinding,在这种情况下,它会给你很大的帮助

票数 1
EN

Stack Overflow用户

发布于 2022-04-24 09:35:37

添加这一行

代码语言:javascript
复制
import android.widget.Button;

FirstActivity.java

代码语言:javascript
复制
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();
            }
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71987041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档