在Android Hello World程序中单击按钮时发生IllegalStateException [英] IllegalStateException when click on button in android hello world program

查看:56
本文介绍了在Android Hello World程序中单击按钮时发生IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android新手,正在尝试实现android Google开发人员网站上提供的MyFirstApp,因此该应用包含一个文本框和按钮,如果您在文本字段中输入任何文本并单击按钮,则它会在屏幕上显示相同的内容,但是当我点击发送按钮时遇到问题.请指导我如何解决这个问题.

I'm new to android and I'm trying to implement MyFirstApp given on android google developers site, So app contains one textbox and button, if you enter any text in textfield and click on button it displays same content on screen, but having problem when i clicked on send button. Please guide me how to solve this problem.

以下例外:

<p>03-15 18:00:03.430: E/AndroidRuntime(592): FATAL EXCEPTION: main</p>
<p>03-15 18:00:03.430: E/AndroidRuntime(592): java.lang.IllegalStateException:Could not find a method MainActivity.sendMessage(View) in the activity class com.example.myfirstapp.DisplayMessageActivity for onClick handler on view class android.widget.Button</p>
<p>03-15 18:00:03.430: E/AndroidRuntime(592):   at android.view.View$1.onClick(View.java:3031)</p>
<p>03-15 18:00:03.430: E/AndroidRuntime(592):   at android.view.View.performClick(View.java:3511)</p>

以下是两项活动:
1.DisplayMessageActivity这是android清单文件中提到的活动.该工具布局部分

Here are two activities:
1.DisplayMessageActivity this is activity mentioned in android manifest file . This implement layout part

       public class DisplayMessageActivity extends Activity {

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

        setContentView(R.layout.activity_display_message_1);

    }
       }

2.MainActivity
这是活动工具sendmessage函数,当用户单击按钮时调用该函数.

2.MainActivity
this is activity implement sendmessage function that is called when users clicked on button .

      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) {
        // Do something in response to button
        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);
    }


}

Android清单xml文件:

Android Manifest xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="DisplayMessageActivity"
            android:label="@string/title_activity_display_message">
           <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> 
        </activity>
    </application>

</manifest>

和布局xml文件:

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

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

推荐答案

更改清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
 <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_display_message">
           <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> 
        </activity>


<activity
            android:name=".DisplayMessageActivity" />
    </application>

</manifest>

DisplayMessgaeActivity :

public class DisplayMessageActivity extends Activity {

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



    }
       }

MainActivity :

public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
  @Override
             protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_display_message_1);
}

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

}


您的布局应具有生命周期方法 onCreate 它应该有一个onCreate方法,您需要设置 setContentview(R.layout.yourlayout)用于MainActivity


your layout should have a life cycle method onCreate It should have a onCreate method and you need to set setContentview(R.layout.yourlayout) for the MainActivity

这篇关于在Android Hello World程序中单击按钮时发生IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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