【问题标题】:onClick method issueonClick 方法问题
【发布时间】:2013-11-27 23:24:27
【问题描述】:

好的,所以我正在浏览 devoloper.android.com 网站上的“MyFirstApp”教程。我在上一个教程,我已经为你可以说的第一章做了一切。唯一的事情是,在“开始另一个活动”课程结束时,它会告诉您运行应用程序。好吧,当我运行它时,它会在模拟器上显示一个可单击的按钮,但是当您单击它时,它会出现运行时错误并强制关闭。我试图对此进行一些研究,但不知道发生了什么。错误是:java.lang.IlleglStateException: 在 android.widet.B 上的视图类的 onClick 处理程序的活动类 com.example.MyFirstApp.MainActivity 中找不到方法 sendmessage(view) em> 我将展示代码:

主活动:

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

}

【问题讨论】:

  • 您的 xml 中有 Button 吗?我认为您没有发布所有内容
  • 你要试试我的回答

标签: android onclick


【解决方案1】:

看起来您在 xml 中将函数名称拼错为 Button onClick()。你的java代码中有一个大写的“m”

 public void sendMessage(View view) {

你的 xml 中可能还有一个小写的“m”

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

为了符合java标准,在你的xml里改成

 android:onClick="sendMessage"/>

【讨论】:

  • 这是否解决了您的问题?
  • 我做了你提到的关于大写字母的事情。我修复了所有这些,但它仍在发生。这是我看到的:imageshack.us/photo/my-images/51/problem3y.png
  • 我在该链接中看不到任何内容。如果您收到错误消息,请发布 logcat
  • 04-05 19:44:14.688: E/AndroidRuntime(599): 致命异常: main 04-05 19:44:14.688: E/AndroidRuntime(599): java.lang.NoClassDefFoundError: android.app.ActionBar 04-05 19:44:14.688: E/AndroidRuntime(599): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:36) 04-05 19:44:14.688: E/AndroidRuntime (599): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  • 04-05 19:44:14.688: E/AndroidRuntime(599): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-05 19:44:14.688: E/ AndroidRuntime(599): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-05 19:44:14.688: E/AndroidRuntime(599): 在 android.app.ActivityThread.access$2300(ActivityThread.java: 125) 04-05 19:44:14.688: E/AndroidRuntime(599): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
【解决方案2】:

我认为您以错误的方式从意图中获取信息。 EXTRA_MESSAGE 是内容,message 是标识符。

所以你的代码在DisplayMessageActivity.java

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

实际上应该是:

String message = intent.getStringExtra(message);

【讨论】:

    【解决方案3】:

    您可以在 activity_main.xml 中看到,您必须以这种方式使用onClick() 声明按钮,然后它才会起作用。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click" 
            android:onClick="sendMessage()"/>
    
    </LinearLayout>
    

    【讨论】:

    • 当我提出建议并解决了这个问题时,OP 确实在你提出这个答案之前 6 小时尝试了这个。但是你不要在 xml 中使用带有函数名的“()”
    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2012-06-23
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多