Emulator crashing on button press in app

I was following the Dev guide on Google's site: http://developer.android.com/training/basics/firstapp/starting-activity.html

But whenever I press the button in my emulator the app crashes. I tried it on my phone and I get the same result. Here is my code The indentation is not working correctly on here Main activity:

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

}

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

My second activity

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

}

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

Logcat:

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

Answers


you have add

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

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

    </activity>

replace it with

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

you have define your Activity twice..May be this is the issue of crash.

EDIT According to your new LogCat

seams like, conflict is in your onclick methode name in activity_main.xml, in your edit_message, you have attribute like android:onClick="send message" replace it with android:onClick="sendMessage"


I see your mistake.

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

Go to your layout/activity_main.xml file and in your Button View make sure that

android:onClick="sendMessage"

is there. According to LogCat you have android:onClick="send message" instead. So there is a unwanted space send + message and Message needs to have its first letter capitalized.

Basically you have named your method name sendMessage(View v) but have referenced it in the XML file as "send message". They are not the same :D

good luck


Instead of:

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

Try, this is in your MainActivity:

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

Need Your Help

How to use unsupported language?

python django localization

I can switch to ru and en, but when i try to switch to hy, nothing happens.