【问题标题】:Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on [duplicate]在 [重复] 上的 onClick 处理程序的活动类 com.example.alexander.mobileapp02.MainActivity 中找不到方法 sendMessage(View)
【发布时间】:2015-04-09 17:38:54
【问题描述】:

我试图在互联网上找到答案,但我找不到。我希望有人知道如何解决这个问题,否则我无法通过这个试用应用程序走得更远。

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

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

当我按下按钮时,出现以下错误: java.lang.IllegalStateException:在视图类 android.widget.Button 上的 onClick 处理程序的活动类 com.example.alexander.mobileapp02.MainActivity 中找不到方法 sendMessage(View) (然后应用程序崩溃 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$1.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$1.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>

活动背景(与任何事情无关,但仍然) 活动背景.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;
    }

}

显示消息活动: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);
    }
}

主要活动

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

教程在此链接末尾 ( ) “您现在可以运行该应用程序。当它打开时,在文本字段中输入一条消息,单击发送,该消息将出现在第二个活动中。”

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

有人可以帮忙吗?

【问题讨论】:

  • 请尝试删除您认为不必要的代码。这太过分了。此外,将您的代码放在代码块中(hightlight 和 Ctrl+K),而不是代码 sn-ps。最后,请先尝试搜索您的错误,因为这个问题经常被问到。
  • Java != JavaScript 仅供参考
  • Javascript 每天启发我们的路径球,@codeMagic

标签: java javascript android xml illegalstateexception


【解决方案1】:

老兄,错误很明显 -

在活动类中找不到方法 sendMessage(View) com.example.alexander.mobileapp02.MainActivity

当你在 XML 中声明这样的一行 -

android:onClick="sendMessage"

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

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

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

【讨论】:

  • 自动 +1 用于寻找阅读代码的动机
  • 啊。掌心时刻。 XDi.imgur.com/2JzA19Y.jpg
  • 关键是PUBLIC关键字。
猜你喜欢
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 2012-07-14
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多