onClick method issue

做~自己de王妃 提交于 2019-12-01 09:38:43

问题


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

}

回答1:


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"/>



回答2:


I think you're getting the message from the intent the wrong way. EXTRA_MESSAGE is the content and message is the identifier.

So your code on DisplayMessageActivity.java

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

should actually be:

String message = intent.getStringExtra(message);



回答3:


You can see in your activity_main.xml you have to declare button with onClick() this way then and then it will be work.

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


来源:https://stackoverflow.com/questions/15845181/onclick-method-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!