Newb
Newb

Reputation: 31

Android development in IntelliJ IDEA -v Eclipse (Starting an Activity)

I am just beginning with developing Android projects and I've run into a road bump. I am starting with the developer site (http://developer.android.com/training/basics/firstapp/index.html) and my problem begins with starting a new Activity (http://developer.android.com/training/basics/firstapp/starting-activity.html)

The site expects Eclipse, and I suspect that I am missing something that happens behind the scenes with Eclipse, but I am using IntelliJ IDEA.

In DisplayMessageAcitivity.java I am getting the following Error:

cannot find symbol variable activity_display_message.

In the Eclipse example, this is a Layout Name.

I am including my code as it was when I first encountered the issue rather than with my attempts at fixes.

DisplayMessageActivity:

public class DisplayMessageActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="13"/>
    <application android:label="@string/app_name" android:icon="@drawable/icon">
        <activity android:name="MyActivity"
                  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="My Message">
                <meta-data android:name="android.support.PARENT_ACTIVITY"
                        android:value="com.example.MainActivity"/>
        </activity>
    </application>
</manifest>

R.java:

package com.example;

public final class R {
    public static final class attr {
    }
    public static final class color {
        public static final int testing_string_color=0x7f040000;
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int edit_message=0x7f060000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f050000;
        public static final int button_send=0x7f050002;
        public static final int edit_message=0x7f050001;
        public static final int menu_settings=0x7f050003;
        public static final int title_activity_main=0x7f050004;
    }
}

main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
        >
    <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>

Upvotes: 2

Views: 760

Answers (3)

M.M
M.M

Reputation: 141574

When following that tutorial but not using Eclipse, the code should actually be:

@Override
public 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);
}

The version with setContentView(R.layout.activity_display_message); is part of some Eclipse boilerplate code; if you added the activity via Eclipse then it would have added that line plus some other support code which makes that line work.

Upvotes: 0

WarrenFaith
WarrenFaith

Reputation: 57672

Replace setContentView(R.layout.activity_display_message); with setContentView(R.layout.main);

You can only reference a layout with the name of the layout file.

Upvotes: 2

Kevin Coppock
Kevin Coppock

Reputation: 134664

Because you don't have an XML file called activity_display_message.

If you call setContentView(R.layout.main), it is referencing an XML layout document titled main.xml. If you're using R.layout.activity_display_message, you have to have a corresponding layout called activity_display_message. If your intent is to use that main.xml layout instead, you should change your call to setContentView(R.layout.main) instead.

Upvotes: 4

Related Questions