1

so I've been going through a beginner's guide for creating Android Applications (http://developer.android.com/training/basics/firstapp/index.html). It has worked fine except for one step which I keep getting errors at.

The MainActivity.java does not work for me. I am getting errors of all kinds on 3 different places.

This is what my code looks like:

package com.fredde.myfirstapp;


public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    import android.app.Activity;enter code here
    import android.content.Intent;
    import android.view.View;
    import android.widget.EditText;
    import android.view.View;



    /** Called when the user clicks the Send button */
    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);
    }

So I'm hoping that someone who has gone through this guide can help me out, or just someone who can see what's wrong despite not having done this particular project. Thanks in advance!

Go easy on me, I'm a complete beginner.

Bhanu Kaushik
  • 876
  • 6
  • 25
  • 1
    I don't know which errors you get but one thing for sure. You're not setting any layout for the activity so the findViewById will surely return null. You'll probably get NullRefException in the next line. I suggest you do more reading on how to implement activities. – Lior Ohana Oct 19 '13 at 21:44
  • How the hell did you got those imports inside your Activity class? – Tudor Luca Oct 19 '13 at 21:45
  • You forget the activity `onCreate` method – ramaral Oct 19 '13 at 21:56
  • What's left now, after copying G V's code, is an error on the line EditText editText = (EditText) findViewById(R.id.edit_message); " R cannot be resolved to a variable" @ramaral – user2898833 Oct 19 '13 at 22:13

5 Answers5

1

import statements should be out of the class.

package com.fredde.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.view.View;

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) {
    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);
}
Crickcoder
  • 2,135
  • 4
  • 22
  • 36
0

Ok, so after copying G V's code there is only one error left: On this line ditText editText = (EditText) findViewById(R.id.edit_message); it says "R cannot be resolved to a variable"

Clean the project and then build it again.

Lia
  • 1
  • 2
0

You need to call onCreate and inside, setCustomView, to inflate a layout to that activity.

@override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCustomView(R.layout.yourLayoutXMLfile);
}

If you do not write this the app doesn't know where to find objects like EditText, etc.

Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34
  • I suppose you mean that I should put this in DisplayMessageActivity.java? I did, and now I'm getting an error saying activity_main cannot be resolved or is not a field @Lorenzo Barbagli sorry for being a complete retard but i'm very new to programming – user2898833 Oct 19 '13 at 22:35
0

I just compiled the following and didn't get any errors. This is what I did, under onCreate method..

setCustomView(R.layout.file.xml);

for this create a file called file.xml under res->layout->file.xml. I assigned an id for Edit view in the xml like :`

 <EditText 
    android:id="@+id/edit_mssg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/msg"/>

I am sure you have a java file called DisplayMesageActivity.java

 package com.fredde.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.view.View;

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCustomView(R.layout.file.xml);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_mssg);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
score
  • 521
  • 1
  • 8
  • 22
  • Can your please take the screen shot of your erros if its too much for you to post errors here... – score Oct 19 '13 at 23:10
  • Ok so here are the errors I'm getting in AndroindManifest.xml, DisplayMessageAcitivty.java and MainActivity.java. Idk if they even make sense to you put like this... completely lost at the moment. @SHANKAR http://i.imgur.com/iWyt3jF.png?1?6503 – user2898833 Oct 19 '13 at 23:26
  • have you defined your activity in AndroidManifest.xml. your second activity as well should be defined: . Right after your previous activity ends at. – score Oct 19 '13 at 23:37
  • Did not change anything... :/ @SHANKAR – user2898833 Oct 19 '13 at 23:40
  • This is how your manifest.xml should look like... i got it straight from the link you provided at the top along with your question: ... – score Oct 19 '13 at 23:42
  • Thanks man, fixed the error in that one! Any idea bout the other two? @SHANKAR – user2898833 Oct 19 '13 at 23:46
  • I have posted one more answer .. check that out – score Oct 19 '13 at 23:53
  • if any one of the answer helped your solve your problem you need to accept the answer and click on up arrow on comments that has helped resolved your problem. Its quite frustrating at the beginning.. you will be ok with the time and more practice... i am also new in Android... – score Oct 20 '13 at 00:00
0

For your main activity:

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

the following has to be defined in strings.xml under res->values->

<resources>
...
<string name="title_activity_display_message">My Message</string>
</resources>

for R can not be resolved: make sure you have defined edit_message as an id for edit view in your xml file like the following:

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

this should solve your problem

score
  • 521
  • 1
  • 8
  • 22