android - 我现在正在研究 android,首先我创建编辑文本,然后将按钮编辑为整数

标签 android performance android-studio

我正在使用 android studio 构建新的应用程序。我创建了按钮的操作。下面是代码 fragment 。

final EditText text = (EditText) findViewById(R.id.editText);
        Button button = (Button) findViewById(R.id.addButton);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button.setOnClickListener(
                new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        try {
                            int myNum = Integer.parseInt(text.getText().toString());
                        } catch(NumberFormatException nfe) {

                        }
                    }
                }
        );

当我尝试运行该模块时,它给出了这条错误消息。

enter image description here 这是什么错误信息?我无法理解这一点。

最佳答案

final EditText text = (EditText) findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.addButton);

将上面几行放在这些行之后

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 final EditText text = (EditText) findViewById(R.id.editText);
 Button button = (Button) findViewById(R.id.addButton);

否则您的 Activity 无法找到 ID 为“editText”和“addButton”的 View 。 通过调用 setContentView(R.layout.activity_main),您是在说您的 Activity ,activity_main 是您 Activity 的布局,您引用的所有 View 都来自此布局。

关于android - 我现在正在研究 android,首先我创建编辑文本,然后将按钮编辑为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069456/

相关文章:

android - 媒体元数据检索器 JNI (14060) : getFrameAtTime: videoFrame is a NULL pointer(Android)

android - 如何通过 wi-fi 从 PC 与 Android 通信?

android - 划分我的 cardview 设计问题

java - Matlab 与 Java

android - 动态更改 Activity 背景

java - 在Android Studio中启动AVD时出错

android - 如何在 android studio 中将 e^z 表示为文本?

android - Ionic cordova构建android失败: Exception in thread "main" java. lang.NullPointerException

c# - 如果我在 MongoDB 上使用 LINQ,为什么会失去性能?

r - 我应该在 R 中使用 crossprod 或基函数来计算两个向量距离的平方范数吗?