找不到在活动课com.example.alexander.mobileapp02.MainActivity为onclick处理程序上的方法的sendMessage(查看) [英] Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on

查看:220
本文介绍了找不到在活动课com.example.alexander.mobileapp02.MainActivity为onclick处理程序上的方法的sendMessage(查看)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在网上寻找答案,但我怎么也找不到它。我希望有人知道如何解决这个ISEA,否则我真的不能再这个试用程序走。

这是我下面的教程,并在那里我结束了: http://developer.android.com/training/basics/firstapp/starting -activity.html (在网站上preSS previous到

我做了一个简单的应用程序,它会到主(第一/家庭)活动,那么你就输入您的姓名和preSS的按钮。 (见图片)。

当我preSS的按钮,这个错误出现: java.lang.IllegalStateException:找不到在活动课com.example.alexander.mobileapp02.MainActivity为onclick处理方法的sendMessage(视图)的视图类android.widget.Button (和应用程序,然后崩溃b.t.w。)

我只是要告诉整个应用程序:

下面完整的错误:

设备驱动程序的API版本:23     用户空间API版本:23 02-09 21:01:14.871 13128-13128 / com.example.alexander.mobileapp02 E /:马里:版本= Linux的r3p2-01rel3 BUILD_DATE =周五03月21日十三点52分五十秒KST 2014年 02-09 21:01:14.956 13128-13128 / com.example.alexander.mobileapp02 D / OpenGLRenderer:启用调试模式0 02-09 21:01:22.961 13128-13139 / com.example.alexander.mobileapp02 D / dalvikvm:GC_FOR_ALLOC释放257K,17%的自由7921K / 9456K,暂停21ms,共21ms 02-09 21:01:39.271 13128-13128 / com.example.alexander.mobileapp02 D / AndroidRuntime:关闭虚拟机 02-09 21:01:39.271 13128-13128 / com.example.alexander.mobileapp02 W / dalvikvm:主题ID = 1:螺纹未捕获异常退出(组= 0x41bd4c08) 02-09 21:01:39.281 13128-13128 / com.example.alexander.mobileapp02 E / AndroidRuntime:致命异常:主要     工艺:com.example.alexander.mobileapp02,PID:13128     java.lang.IllegalStateException:找不到在活动课com.example.alexander.mobileapp02.MainActivity为onclick处理方法的sendMessage(视图)的视图类android.widget.Button             在android.view.View $ 1.onClick(View.java:3970)             在android.view.View.performClick(View.java:4654)             在android.view.View $ PerformClick.run(View.java:19438)             在android.os.Handler.handleCallback(Handler.java:733)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:146)             在android.app.ActivityThread.main(ActivityThread.java:5602)             在java.lang.reflect.Method.invokeNative(本机方法)             在java.lang.reflect.Method.invoke(Method.java:515)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)             在dalvik.system.NativeStart.main(本机方法)      java.lang.NoSuchMethodException:产生的原因的sendMessage [类android.view.View]             在java.lang.Class.getConstructorOrMethod(Class.java:472)             在java.lang.Class.getMethod(Class.java:857)             在android.view.View $ 1.onClick(View.java:3963) 在android.view.View.performClick(View.java:4654) 在android.view.View $ PerformClick.run(View.java:19438) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:146) 在android.app.ActivityThread.main(ActivityThread.java:5602) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本机方法) 02-09 21:01:46.121 13128-13128 / com.example.alexander.mobileapp02 I /工艺:发送信号。 PID:13128 SIG:9

下面的fragment_main.xml:

<的LinearLayout     的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     的xmlns:工具=htt​​p://schemas.android.com/tool​​s     机器人:方向=横向     机器人:layout_width =match_parent     机器人:layout_height =match_parent     机器人:以下属性来=@扪/ activity_horizo​​ntal_margin     机器人:paddingRight =@扪/ activity_horizo​​ntal_margin     机器人:paddingTop =@扪/ activity_vertical_margin     机器人:paddingBottom会=@扪/ activity_vertical_margin     工具:上下文=。MainActivity $ PlaceholderFragment     机器人:后台=#A9F5F2>     < EditText上机器人:ID =@ + ID / edit_message         机器人:layout_weight =1         机器人:layout_width =0dp         机器人:layout_height =WRAP_CONTENT         机器人:提示=@字符串/ edit_message/>     <按钮         机器人:layout_width =WRAP_CONTENT         机器人:layout_height =WRAP_CONTENT         机器人:文本=@字符串/ button_send         机器人:的onClick =的sendMessage/>     < / LinearLayout中>

这是activity_display_message.xml

< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com / APK / RES /安卓     的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:layout_width =match_parent     机器人:layout_height =match_parent机器人:以下属性来=@扪/ activity_horizo​​ntal_margin     机器人:paddingRight =@扪/ activity_horizo​​ntal_margin     机器人:paddingTop =@扪/ activity_vertical_margin     机器人:paddingBottom会=@扪/ activity_vertical_margin     工具:上下文=com.example.alexander.mobileapp02.DisplayMessageActivity>     < TextView的Andr​​oid版本:文本=@字符串/参考hello world机器人:layout_width =WRAP_CONTENT         机器人:layout_height =WRAP_CONTENT/> < / RelativeLayout的>

这是:activity_main.xml

<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com / APK / RES /安卓 的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:ID =@ + ID /容器 机器人:layout_width =match_parent机器人:layout_height =match_parent MainActivity:工具上下文=工具:忽略=MergeRootFrame/>

字符串:的strings.xml

< XML版本=1.0编码=UTF-8? > <资源>     <字符串名称=APP_NAME> MobileApp02< /串>     <字符串名称=参考hello world>世界,你好<!/串>     <字符串名称=action_settings>设置< /串>     <字符串名称=例题> example2string< /串>     <字符串名称=edit_message>输入您的姓名请< /串>     <字符串名称=button_send> START:D< /串>     <字符串名称=title_activity_main> MainActivity< /串>     <字符串名称=title_activity_display_message>我的留言和LT; /串> < /资源>

活动的背景(有无关,与任何事情,但仍然) activitybackground.java:

包com.example.alexander.mobileapp02; 进口android.os.Bundle; 进口android.app.Activity; 进口android.view.Menu; //开始另一个activty 公共类activitybackground延伸活动{     @覆盖     保护无效的onCreate(包savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.activity_main);     }     @覆盖     公共布尔onCreateOptionsMenu(功能菜单){         //充气菜单;这增加了项目操作栏,如果它是present。         。getMenuInflater()膨胀(R.menu.menu_main,菜单);         返回true;     } }

显示消息活动:DisplayMessageActivity.java:

包com.example.alexander.mobileapp02; 进口android.content.Intent; 进口android.support.v7.app.ActionBarActivity; 进口android.os.Bundle; 进口android.view.Menu; 进口android.view.MenuItem; 进口android.widget.TextView; 公共类DisplayMessageActivity扩展ActionBarActivity {     @覆盖     保护无效的onCreate(包savedInstanceState){         super.onCreate(savedInstanceState);         //setContentView(R.layout.activity_display_message);         //从意图获取信息         意向意图= getIntent();         字符串消息= intent.getStringExtra(MyActivity.EXTRA_MESSAGE);         //创建文本视图         TextView中的TextView =新的TextView(本);         textView.setTextSize(40);         textView.setText(消息);         //设置文本视图作为活动布局         的setContentView(TextView的);     }     / *     @覆盖     公共布尔onCreateOptionsMenu(功能菜单){         //充气菜单;这增加了项目操作栏,如果它是present。         。getMenuInflater()膨胀(R.menu.menu_display_message,菜单);         返回true;     }     // * /     @覆盖     公共布尔onOptionsItemSelected(菜单项项){         //处理动作栏项目点击这里。将操作栏         //自动在主/向上按钮操作的点击,只要         //你在AndroidManifest.xml中指定一个父活动。         INT的id = item.getItemId();         // noinspection SimplifiableIfStatement         如果(ID == R.id.action_settings){             返回true;         }         返回super.onOptionsItemSelected(项目);     } }

主要活动

MainActivity.java:

包com.example.alexander.mobileapp02; 进口android.content.Intent; 进口android.support.v7.app.ActionBarActivity; 进口android.support.v7.app.ActionBar; 进口android.support.v4.app.Fragment; 进口android.os.Bundle; 进口android.view.LayoutInflater; 进口android.view.Menu; 进口android.view.MenuItem; 进口android.view.View; 进口android.view.ViewGroup; 进口android.os.Build; 进口android.widget.ImageButton; 进口android.widget.Toast; 公共类MainActivity扩展ActionBarActivity {     @覆盖     保护无效的onCreate(包savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.activity_main);         configureImageButton();         如果(savedInstanceState == NULL){             getSupportFragmentManager()的BeginTransaction()                     。新增(R.id.container,新PlaceholderFragment())                     。承诺();         }         / *         的ImageButton按钮=(的ImageButton)findViewById(R.id.imageButton);         button.setOnClickListener(新View.OnClickListener(){             公共无效的onClick(视图v){                 startActivity(新意图(MainActivity.this,activitybackground.class));             }         });         // * /     }     //}     私人无效configureImageButton(){         / *         的ImageButton BTN =(的ImageButton)findViewById(R.id.imageButton);         btn.setOnClickListener(新View.OnClickListener(){             @覆盖             公共无效的onClick(视图v){                 Toast.makeText(MainActivity.this,你单击该按钮!,Toast.LENGTH_LONG.show());                 //改变图像按钮123                 的ImageButton BTN =(的ImageButton)findViewById(R.id.imageButton);                 btn.setImageResource(R.drawable.example_two);             }         });      // * /         / *         的ImageButton按钮=(的ImageButton)findViewById(R.id.imageButton);         button.setOnClickListener(新View.OnClickListener(){             公共无效的onClick(视图v){                //                 的ImageButton BTN =(的ImageButton)findViewById(R.id.imageButton);                 btn.setImageResource(R.drawable.example_two);             }         });         // * /     }     @覆盖     公共布尔onCreateOptionsMenu(功能菜单){         // 123         //充气菜单;这增加了项目操作栏,如果它是present。         。getMenuInflater()膨胀(R.menu.menu_main,菜单);         返回true;     }     @覆盖     公共布尔onOptionsItemSelected(菜单项项){         //处理动作栏项目点击这里。将操作栏         //自动在主/向上按钮操作的点击,只要         //你在AndroidManifest.xml中指定一个父活动。         INT的id = item.getItemId();         // noinspection SimplifiableIfStatement         如果(ID == R.id.action_settings){             返回true;         }         返回super.onOptionsItemSelected(项目);     }     / **      *包含一个简单的视图的占位符片段。      * /     公共静态类PlaceholderFragment扩展片段{         公共PlaceholderFragment(){         }         @覆盖         公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,                                  捆绑savedInstanceState){             查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);             返回rootView;         }     } }

和MyActivity.java:

包com.example.alexander.mobileapp02; 进口android.content.Intent; 进口android.support.v7.app.ActionBarActivity; 进口android.view.View; 进口android.widget.EditText; 进口android.app.Activity; 进口android.content.Intent; 进口android.os.Bundle; 进口android.view.Menu; 进口android.widget.EditText; 公共类MyActivity扩展ActionBarActivity {     公共最后静态字符串EXTRA_MESSAGE =com.example.alexander.mobileapp02.MESSAGE;     / ** pressing启动按钮* /     公共无效的sendMessage(查看视图){         意向意图=新的意图(这一点,DisplayMessageActivity.class);         EditText上EDITTEXT =(EditText上)findViewById(R.id.edit_message);         字符串消息= editText.getText()的toString()。         intent.putExtra(EXTRA_MESSAGE,消息);         startActivity(意向);     } }

现在阅读: fragment_main.xml显示按钮,在这里你可以输入你的名字。

activity_main.xml是没有什么(只是用一个空白页的Hello world!

现在,这是非常重要的。 activity_display_message.xml

本教程的状态,在这个环节(年底)的你现在可以运行应用程序。当它打开时,键入一条消息在文本字段中,点击发送,并在第二个活动显示的消息。

但是,当我输入的东西,preSS按钮我的应用程序崩溃,并且得到上面键入错误。

可有人帮忙吗?

解决方案

老兄,错误的是明确的 -

  

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

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

 安卓的onClick =的sendMessage
 

您需要创建获取你的 MainActivity ,它需要执行的方法查看调用它作为的参数。

 公共无效的sendMessage(查看MyView的)
{
    //你的code在这里
}
 

添加这在 MainActivity ,不是你的 MyActivity 并填写code,做了的sendMessage 。这不是魔术。

I tried to find the answer on internet, but I could not find it. I hope somebody knows how to solve this isea, since otherwise I cannot really go further with this tryout app.

This is the tutorial I follow and where I ended up: http://developer.android.com/training/basics/firstapp/starting-activity.html (on the site press previous to

I made a simple app, it goes to the main (first / home) activity, then you enter your name and press the button. (see images).

When I press the button, this error comes: 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 (and the app then crashes b.t.w.)

I am just going to show the entire app:

Here the full error:

    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

Here the 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>

And this is the 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>

this is: 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" />

the strings: 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>

the activity background (which has nothing to do with anything yet, but still) 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;
    }

}

the display message 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);
    }
}

the main 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;
        }
    }
}

and 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);
    }
}

Now read this: fragment_main.xml shows the button and where you can enter your name.

activity_main.xml is nothing really (just an empty page with "Hello world!"

Now this is important. activity_display_message.xml

The tutorial states at the end of this 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."

But when I type something and press the button my app crashes and gets the error typed above.

Can anybody help please ?

解决方案

Dude, the error is clear -

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

When you declare in XML a line like this -

android:onClick="sendMessage"

You need to create the method that gets executed in your MainActivity, it takes the View that called it as a parameter.

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

Add this in your MainActivity, not your MyActivity and fill out the code that does the sendMessage. It's not magic.

这篇关于找不到在活动课com.example.alexander.mobileapp02.MainActivity为onclick处理程序上的方法的sendMessage(查看)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆