0

我正在关注 Google 网站上的开发指南:http: //developer.android.com/training/basics/firstapp/starting-activity.html

但是每当我按下模拟器中的按钮时,应用程序就会崩溃。我在手机上试了一下,结果是一样的。这是我的代码这里的缩进不能正常工作主要活动:

package com.example.rishubs.app;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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.myapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

/** Called when Button is pressed */
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);
}

}

我的 XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rishubs.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
         android:minSdkVersion="14"
         android:targetSdkVersion="15" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
        android:label="@string/app_name">

        </activity>
     </application>

</manifest>

我的第二个活动

package com.example.rishubs.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get Message from 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);

        setContentView(textView);
    }

}

更新的 XML:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.rishubs.app"
     android:versionCode="1"
     android:versionName="1.0" >

     <uses-sdk
         android:minSdkVersion="14"
         android:targetSdkVersion="15" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
        android:label="@string/app_name">

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

        </activity>
    </application>

</manifest>

日志猫:

07-15 18:48:26.605: E/AndroidRuntime(1454): FATAL EXCEPTION: main 07-15 18:48:26.605: E/AndroidRuntime(1454): java.lang.IllegalStateException: Could not find a method send message(View) in the activity class com.example.rishubs.app.MainActivity for onClick handler on view class android.widget.Button 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View$1.onClick(View.java:3031) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View.performClick(View.java:3511) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View$PerformClick.run(View.java:14105) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.os.Handler.handleCallback(Handler.java:605) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.os.Handler.dispatchMessage(Handler.java:92) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.os.Looper.loop(Looper.java:137) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.app.ActivityThread.main(ActivityThread.java:4424) 07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.reflect.Method.invokeNative(Native Method) 07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.reflect.Method.invoke(Method.java:511) 07-15 18:48:26.605: E/AndroidRuntime(1454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 07-15 18:48:26.605: E/AndroidRuntime(1454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 07-15 18:48:26.605: E/AndroidRuntime(1454): at dalvik.system.NativeStart.main(Native Method) 07-15 18:48:26.605: E/AndroidRuntime(1454): Caused by: java.lang.NoSuchMethodException: send message [class android.view.View] 07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.Class.getConstructorOrMethod(Class.java:460) 07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.Class.getMethod(Class.java:915) 07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View$1.onClick(View.java:3024) 07-15 18:48:26.605: E/AndroidRuntime(1454): ... 11 more

4

3 回答 3

2

我看到了你的错误。

java.lang.IllegalStateException: Could not find a method send message(View) in the activity class com.example.rishubs.app.MainActivity for onClick handler on view class android.widget.Button

转到您的 layout/activity_main.xml 文件并在您的按钮视图中确保

android:onClick="sendMessage"

在那儿。根据 LogCat 你有 android:onClick="send message" 代替。所以有一个不需要的空间 send + message 并且 Message 的第一个字母需要大写。

基本上,您已将方法名称命名为 sendMessage(View v),但已在 XML 文件中将其引用为“发送消息”。他们不一样 :D

祝你好运

于 2012-11-18T16:59:59.973 回答
0

你有添加

<activity
        android:name=".DisplayMessageActivity"
    android:label="@string/app_name">

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

    </activity>

将其替换为

<activity
        android:name=".DisplayMessageActivity"
    android:label="@string/app_name"> </activity>

你已经定义了你的Activity两次..可能这是崩溃的问题。

编辑根据您的新 LogCat

接缝之类的,冲突在您的 onclick 方法名称中activity_main.xml,在您的中edit_message,您有属性,例如android:onClick="send message"将其替换为android:onClick="sendMessage"

于 2012-07-15T18:28:30.697 回答
-3

代替:

Intent intent = new Intent(this, DisplayMessageActivity.class);

试试看,这是在您的 MainActivity 中:

Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
于 2012-07-15T18:11:04.187 回答