的onClick方法问题 [英] onClick method issue

查看:181
本文介绍了的onClick方法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我会通过MyFirstApp对devoloper.android.com网站上的教程。我在上一篇教程,我已经在第一章中,你可以说八九不离十做的一切。唯一的一点是,在开始另一项活动课结束,到了最后它会告诉你运行应用程序。好吧,当我运行它,它看起来与在模拟器上可点击的按钮,但是当你点击它,它就会运行时错误和部队密切。我试图做一些这方面的研究,而不是对正在发生的事情的线索。错误是: java.lang.IlleglStateException:找不到方法的SendMessage(视图)在活动课com.example.MyFirstApp.MainActivity为onclick处理程序上的视图类的android.widet.B 我将显示$ C $下这样的:

MainActivity:

 包com.example.myfirstapp;
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;
进口android.widget.EditText;

公共类MainActivity延伸活动{
    公共最后静态字符串EXTRA_MESSAGE =com.example.myfirstapp.MESSAGE;

    / **当用户点击发送按钮调用* /
    公共无效的sendMessage(查看视图){
        意向意图=新的意图(这一点,DisplayMessageActivity.class);
        EditText上EDITTEXT =(EditText上)findViewById(R.id.edit_message);
        字符串消息= editText.getText()的toString()。
        intent.putExtra(EXTRA_MESSAGE,消息);
        startActivity(意向);
        //做一些回应按钮
    }

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

}
 

activity_display_message:

 < RelativeLayout的的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
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=DisplayMessageActivity。>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/参考hello world/>

< / RelativeLayout的>
 

DisplayMessageActivity.java:

 包com.example.myfirstapp;

进口android.annotation.Sup pressLint;
进口android.annotation.TargetApi;
进口android.app.ActionBar;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Build;
进口android.os.Bundle;
进口android.support.v4.app.NavUtils;
进口android.view.MenuItem;
进口android.widget.TextView;

公共类DisplayMessageActivity延伸活动{

    @燮pressLint(NewApi)
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        //从意图消息
        意向意图= getIntent();
        字符串消息= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        //创建文本视图
        TextView中的TextView =新的TextView(本);
        textView.setTextSize(40);
        textView.setText(消息);

        //设置文本视图作为活动布局
        的setContentView(TextView的);

        //确保我们在运行油炒或更高版本使用动作条的API
        如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.FROYO){
            //显示操作栏中的向上按钮。
            getupActionBar()setDisplayHomeAsUpEnabled(真)。
        }
    }

    私有动作条getupActionBar(){
        // TODO自动生成方法存根
        返回null;
    }

    / **
     *设置{@link android.app.ActionBar},如果API是可用的。
     * /
    @TargetApi(Build.VERSION_ codeS.HONEYCOMB)
    私人无效setupActionBar(){
        如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
            getActionBar()setDisplayHomeAsUpEnabled(真)。
        }
    }


    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            //这个ID再presents主页或向上按钮。在这种情况下
            //活动,显示向上按钮。使用NavUtils允许用户
            //浏览在应用结构中的一个级别。对于
            //更多详细信息,请参阅在Android上设计导航模式:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(本);
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }

}
 

解决方案

它看起来就像你在你的XML拼写错误的功能名称按钮的onClick()。你有一个大写的M在你的java code

 公共无效的sendMessage(查看视图){
 

和可能是一个小写字母M,在你的XML

 <按钮
...
机器人:的onClick =SendMessage函数/>
 

要符合Java标准,在你的XML更改为

 安卓的onClick =的sendMessage/>
 

ok, so I am going through the tutorial of the "MyFirstApp" on the devoloper.android.com site. I am on the last tutorial and I have did everything for the first chapter you can sorta say. The only thing is, at the end of "start another activity" lesson, at the end it tells you to run the app. Well when I run it, it appears with a clickable button on the emulator, but when you click it, it gets a runtime error and forces close. I have tried to do some research on this, but not a clue on what is going on. The error is: java.lang.IlleglStateException: Could not find a method sendmessage(view) in the activity class com.example.MyFirstApp.MainActivity for onClick handler on view class on android.widet.B I am going to show the code for this:

MainActivity:

package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";    

    /** Called when the user clicks the Send 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);
        // Do something in response to button
    }

    @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.main, menu);
        return true;
    }

}

activity_display_message:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

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

</RelativeLayout>

DisplayMessageActivity.java:

package com.example.myfirstapp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

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

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.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);

        // Make sure we're running on Fry or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            // Show the Up button in the action bar.
            getupActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    private ActionBar getupActionBar() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

解决方案

It looks like you misspelled the function name in your xml for the Button onClick(). You have an uppercase "m" in your java code

 public void sendMessage(View view) {

and probably a lowercase "m" in your xml

<Button
...
android:onClick="sendmessage"/>

To comply with java standards, change it in your xml to

 android:onClick="sendMessage"/>

这篇关于的onClick方法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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