单击按钮时找不到方法

时间:2017-08-01 02:40:38

标签: android

我遇到了一个问题,我希望通过单击按钮将消息从一个布局(activity_main)发送到另一个布局(activity_display_message)。但是点击它时会产生错误。 Android尝试调用onClick方法public void sendMessage(View视图)但无法找到它(尽管它在MainActivity中声明)。 Android Monitor提供的错误是:

  

无法在父级或。中找到方法sendMessage(MainActivity)(View)   android的ancestor Context:在视图类上定义的onClick属性   android.support.v7.widget.AppCompatButton,id为'button'

我的代码详情如下。

MainActivity.java

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.lunatello.bdsapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    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);


    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context="com.example.lunatello.bdsapp.MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:onClick="sendMessage (MainActivity)"
            android:text="@string/button_send"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/button_send" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="183dp"
            android:layout_height="40dp"
            android:layout_marginLeft="16dp"
            android:hint="@string/edit_message"
            android:textColor="@android:color/black"
            android:textColorHint="?attr/colorControlHighlight"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

DisplayMessageActivity.java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;


public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(message);
    }
}

3 个答案:

答案 0 :(得分:1)

将xml更改为以下内容:

 <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:onClick="sendMessage"
            android:text="@string/button_send"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/button_send" />

答案 1 :(得分:0)

只需将android:onClick="sendMessage"放在布局上,无需提供视图。因为视图是自动提供的。

答案 2 :(得分:0)

XML正在请求一个名为sendMessage (MainActivity)的方法,该方法接受View参数,其中的方法不能包含括号或空格。

您有public void sendMessage(View view){,因此onClick应该使用该方法名称。活动无关紧要,因为您的布局可以加载到任何活动或视图中。