简体   繁体   English

单击android hello world程序中的按钮时出现IllegalStateException

[英]IllegalStateException when click on button in android hello world program

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. 我是android的新手,我正在尝试在Android谷歌开发者网站上实现MyFirstApp,所以app包含一个文本框和按钮,如果你在textfield中输入任何文本并点击按钮它会在屏幕上显示相同的内容,但是有问题当我点击发送按钮。 Please guide me how to solve this problem. 请指导我如何解决这个问题。

Following exceptions: 以下例外:

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

Here are two activities: 这是两个活动:
1.DisplayMessageActivity this is activity mentioned in android manifest file . 1.DisplayMessageActivity这是android清单文件中提到的活动。 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 2.MainActivity
this is activity implement sendmessage function that is called when users clicked on button . 这是用户点击按钮时调用的活动实现sendmessage函数。

      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 Manifest xml file: Android Manifest xml文件:

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

And Layout xml file: 和布局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>

Edit: 编辑:

Change your Manifest: 改变你的清单:

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

}

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 你的布局应该有一个生命周期方法onCreate它应该有一个onCreate方法,你需要为MainActivity设置setContentview(R.layout.yourlayout)

Your app is looking for the sendMessage() method in an activity where the method is not implimented 您的应用程序正在查找方法未实现的活动中的sendMessage()方法

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

is this the xml layout name activity_display_message_1 where you declare the Button? 这是你声明Button的xml布局名称activity_display_message_1吗? you must put the method sendMessage also in the same activity 您必须将方法sendMessage也放在同一个活动中

EDIT: FULL SOLUTION 编辑:完整的解决方案

activity_main.xml activity_main.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" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
</LinearLayout>

activity_display_message.xml activity_display_message.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" />
</LinearLayout>

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


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

DisplayMessageActivity.java DisplayMessageActivity.java

  public class DisplayMessageActivity extends Activity {

    @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message_1);


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

} }

Update also you manifest file because you've inverse the launcher 更新你清单文件,因为你已经反转了启动器

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

You are using two time setContentView() . 您正在使用两次setContentView() You can use only one setContentView . 您只能使用一个setContentView

//Just change your manifest file, its not getting path of your "DisplayMessageActivity" for //sample as below : //只需更改清单文件,而不是获取//示例的“DisplayMessageActivity”路径,如下所示:

 android:name="com.example.checkproject.DisplayMessageActivity"

or 要么

android:name =".DisplayMessageActivity"
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);
    this.startActivity(intent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM