在Android入门程序中点击按钮时出现IllegalStateException异常

4

我是新手学习安卓技术,正在尝试实现安卓谷歌开发者网站上的MyFirstApp。应用程序包含一个文本框和按钮,如果你在文本字段中输入任何文本并点击按钮,则会在屏幕上显示相同的内容,但当我点击发送按钮时,出现了问题。

请指导我如何解决这个问题。

以下是异常:

<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清单文件中提到的活动。它实现了布局部分。

       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
这是一个Activity,实现了一个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清单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>

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

读取错误。Android 正在寻找 'MainActivity.sendMessage(View)',而不是 'MainActivity.sendMessage()'。 - njzk2
@njzk2 实际上,我已经编辑了代码,因为有一个用户说你应该使用sendMessage(),而你在xml中使用了sendMessage。但无论如何,那是愚蠢的回答。 - Avinesh
5个回答

1

编辑:

更改您的清单文件:

<?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。对于MainActivity,它应该有一个onCreate方法,并且您需要设置setContentview(R.layout.yourlayout)


我也尝试过了,但是出现了相同的异常,我认为问题不同。从异常信息可以看出——在DisplayMessageActivity中找不到sendMessage方法,但是该方法在MainActivity中已经定义了。 - Avinesh
@neel,请更新您的问题并附上您尝试过的新代码。同时请提供logcat跟踪记录。 - Pragnani
更新了,现在你可以尝试。如果您能提供具体的解决方案将非常有帮助。 - Avinesh
@neel 我已经更新了我的回答,请查看一下,将你的清单文件改成我所发布的内容,你的活动应该在清单文件中进行注册。 - Pragnani
@neel 你试过了吗?这是你正在寻找的具体解决方案。别忘了通过点击答案旁边的正确标记来接受它作为答案。 - Pragnani
显示剩余4条评论

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

0

//只需要更改您的清单文件,它没有获取您的“DisplayMessageActivity”的路径,示例如下:

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

或者

android:name =".DisplayMessageActivity"

0

您的应用正在寻找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): 

这是您声明按钮的 xml 布局名称 activity_display_message_1 吗? 您还必须在同一活动中添加 sendMessage 方法。

编辑:完整解决方案

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

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

主活动

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

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

}

同时更新您的清单文件,因为您已经反转了启动器

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

你说得没错,但我是按照谷歌网站上的说明进行操作的 - http://developer.android.com/training/basics/firstapp/starting-activity.html - Avinesh
@neel 在谷歌的示例中有两个 XML 布局,一个包含实现 onclick 监听器的按钮,另一个应该在单击时调用。 - Festus Tamakloe
@FestusTamakloe 它只有一个布局,对于第二个活动直接设置 TextView 兄弟即可。 - Pragnani

0

您正在使用两个 setContentView()。您只能使用一个 setContentView


1
应该是一条注释而不是答案(主要是因为它实际上并没有“回答”问题)。 - njzk2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接