0

When I make my MyFirstApp project (which I named SampleNotes by the way), I run it on my rooted with CyanogenMod 10.1 Nook HD+ using built-in ADB over wifi, I try to click the send button, and it just doesn't do anything at all; as if I never added anything about intents or the second activity. Can anyone help?

MainActivity.java:

package com.example.samplenotes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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.SampleNotes.MESSAGE";

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

}

AndroidManifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.samplenotes.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="com.example.samplenotes.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.SampleNotes.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.SampleNotes.MainActivity" />
        </activity>
    </application>

</manifest>

DisplayMessageActivity.java:

package com.example.samplenotes;

import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Get message from intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(R.layout.activity_display_message);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:orientation="horizontal" >

        <EditText android:id="@+id/edit_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            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>

activity_display_message.xml:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SampleNotes</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="title_activity_display_message">My Message</string>

</resources>
George
  • 15
  • 1
  • 3

3 Answers3

1

As others have said, to start activity, you need to call:

startActivity();

Moreover, in your DisplayMessageActivity, before you manipulate any controls, you must first set your layout:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //first, set main layout
    setContentView(R.layout.activity_display_message);

then get your text view and set it:

    //Get message from intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = (TextView) findViewById(R.id.text);
    textView.setTextSize(40);
    textView.setText(message);

And add id in your activity_display_message.xml, so that your textView can be retrieved:

    <TextView
    android:oid="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
Melquiades
  • 8,496
  • 1
  • 31
  • 46
0

You don't actually start an activity in the send function:

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

//add this to start your activity
startActivity(intent);

official guide

Melquiades
  • 8,496
  • 1
  • 31
  • 46
cbrulak
  • 15,436
  • 20
  • 61
  • 101
  • Now, it launches the activity, but all the activity does is prints hello world, not the text typed into the EditText. How could I fix this? – George Dec 23 '13 at 20:01
  • In the second activity you need to use the data passed in through the intent to get it. If you keep reading through the Android guides you'll see it. – cbrulak Dec 23 '13 at 20:04
  • ...I added startActivity(intent), even before; the previous code gave me an error, so I followed the recommendation. Unfortunately, it prints hello world like before. – George Dec 23 '13 at 20:05
  • take a look at: http://stackoverflow.com/questions/4308232/passing-data-through-intent-and-receiving-it – cbrulak Dec 23 '13 at 20:06
  • ...how does that apply? – George Dec 23 '13 at 20:44
  • 1) the OP is doing exactly what you want to do as well 2) the accepted answer answers both questions 3) shows you that most of your questions and their answers have already been answered. :) – cbrulak Dec 23 '13 at 20:46
  • I don't understand... sorry i'm an ultra-n00b that has done python and is only starting C; this is java, so it might be too complex. – George Dec 23 '13 at 20:50
  • 1
    Okay, no worries. Here (http://www.vogella.com/articles/AndroidIntent/article.html) is someone is can do a much better job of explaining. Seriously, he's awesome and the content is very rich. Helped me tremendously. Feel free to post follow up questions too, just make one for each problem :) Good luck! – cbrulak Dec 23 '13 at 21:06
0

Add this line at the end of your sendMessage function:

startActivity(intent);

And remove the line setContentView.. from the onCreate of the second activity.

AabidMulani
  • 2,325
  • 1
  • 28
  • 47
  • ...now it doesn't say hello world, it says nothing. It does transfer to the 2nd activity, however. – George Dec 23 '13 at 20:41