有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java MyFirstApp教程Android Studio sendMessage问题

所以我刚开始学习Android Studio for Android development,并开始在他们的网站上学习myFirstApp教程。我正在尝试向按钮添加一个方法,但无法使其工作。我在MainActivity中有sendMessage方法。java,当我从“点击”下拉列表中选择它时,它不会出现。我也有正确的进口商品。有人知道为什么会这样吗?谢谢

下面是我的代码:

package com.example.tyler.myfirstapp;

import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /** Called when the user taps the Send button */
    public void sendMessage(View view) {
    // Do something in response to button
    }
}

共 (1) 个答案

  1. # 1 楼答案

    我也被这个问题困住了,所以我跳过了页面,抓取了MainActivity的完整代码。java(除了我保留的第一行)。 一旦我替换了它,我就会在onClick按钮属性上显示sendMessage[MainActivity]

    package com.example.your....
    
    public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    /** Called when the user taps the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.editText);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
    }