无法在 activity class com.example.alexander.mobileapp02.MainActivity 中为 onClick 处理程序找到方法 sendMessage(View)

Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on

我试图在网上找到答案,但找不到。我希望有人知道如何解决这个 isea,否则我无法真正进一步使用这个试用应用程序。

这是我遵循的教程以及我最终的结果: http://developer.android.com/training/basics/firstapp/starting-activity.html (在网站上按

之前的

我制作了一个简单的应用程序,它转到主(第一个/主页)activity,然后输入您的姓名并按下按钮。 (见图片)。

当我按下按钮时,出现以下错误: java.lang.IllegalStateException: 在 activity class com.example.alexander.mobileapp02.MainActivity 中找不到方法 sendMessage(View) for onClick handler on view class android.widget.Button (然后应用程序崩溃 b.t.w。)

我将展示整个应用程序:

这里是完整的错误:

    Device driver API version: 23
    User space API version: 23
02-09 21:01:14.871  13128-13128/com.example.alexander.mobileapp02 E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
02-09 21:01:14.956  13128-13128/com.example.alexander.mobileapp02 D/OpenGLRenderer﹕ Enabling debug mode 0
02-09 21:01:22.961  13128-13139/com.example.alexander.mobileapp02 D/dalvikvm﹕ GC_FOR_ALLOC freed 257K, 17% free 7921K/9456K, paused 21ms, total 21ms
02-09 21:01:39.271  13128-13128/com.example.alexander.mobileapp02 D/AndroidRuntime﹕ Shutting down VM
02-09 21:01:39.271  13128-13128/com.example.alexander.mobileapp02 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bd4c08)
02-09 21:01:39.281  13128-13128/com.example.alexander.mobileapp02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.alexander.mobileapp02, PID: 13128
    java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on view class android.widget.Button
            at android.view.View.onClick(View.java:3970)
            at android.view.View.performClick(View.java:4654)
            at android.view.View$PerformClick.run(View.java:19438)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
            at java.lang.Class.getConstructorOrMethod(Class.java:472)
            at java.lang.Class.getMethod(Class.java:857)
            at android.view.View.onClick(View.java:3963)
            at android.view.View.performClick(View.java:4654)
            at android.view.View$PerformClick.run(View.java:19438)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
02-09 21:01:46.121  13128-13128/com.example.alexander.mobileapp02 I/Process﹕ Sending signal. PID: 13128 SIG: 9

这里是fragment_main.xml:

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment"
    android:background="#A9F5F2">

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

   

</LinearLayout>

这是 activity_display_message.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.alexander.mobileapp02.DisplayMessageActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是:activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />

字符串:strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MobileApp02</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="example2">example2string</string>
    <string name="edit_message">Enter your name please</string>
    <string name="button_send">START :D</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">My Message</string>

</resources>

activity 背景(目前还没有任何关系,但仍然如此) activitybackground.java :

package com.example.alexander.mobileapp02;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


//starting another  activty

public class activitybackground extends Activity {

    @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;
    }

}

显示信息 activity: DisplayMessageActivity.java :

package com.example.alexander.mobileapp02;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class DisplayMessageActivity extends ActionBarActivity {

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

        //get message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

        //create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        //set the text view   as   the activity layout
        setContentView(textView);
    }


    /*
    @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_display_message, 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);
    }
}

主要activity

MainActivity.java :

package com.example.alexander.mobileapp02;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ImageButton;
import android.widget.Toast;



public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        configureImageButton();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        /*
        ImageButton button= (ImageButton) findViewById(R.id.imageButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,activitybackground.class));
            }
        });
        //*/
    }

    //}


    private void configureImageButton() {

        /*
        ImageButton btn = (ImageButton) findViewById(R.id.imageButton);


        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You clicked the button!", Toast.LENGTH_LONG.show());

                // change image on button 123
                ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
                btn.setImageResource(R.drawable.example_two);
            }
        });
     //*/


        /*
        ImageButton button = (ImageButton) findViewById(R.id.imageButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               //
                ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
                btn.setImageResource(R.drawable.example_two);
            }
        });
        //*/
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 123
        // 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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}

和MyActivity.java:

package com.example.alexander.mobileapp02;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;

public class MyActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.alexander.mobileapp02.MESSAGE";

    /** Pressing the start button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

现在读这个: fragment_main.xml 显示按钮以及您可以输入姓名的位置。

activity_main.xml 什么都不是(只是一个带有 "Hello world!"

的空白页面

现在这很重要。 activity_display_message.xml

教程在本教程末尾说明 link ( ) "You can now run the app. When it opens, type a message in the text field, click Send, and the message appears on the second activity."

但是当我输入内容并按下按钮时,我的应用程序崩溃并收到上面输入的错误。

有人可以帮忙吗?

老兄,错误很明显 -

Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity

当您在 XML 中声明这样一行时 -

android:onClick="sendMessage"

您需要创建在 MainActivity 中执行的方法,它将调用它的 View 作为参数。

public void sendMessage(View myView)
{
    //Your code here
}

将此添加到您的 MainActivity,而不是您的 MyActivity,然后填写执行 sendMessage 的代码。这不是魔法。