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.

asked Oct 19, 2013 at 21:38
4
  • 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. Commented Oct 19, 2013 at 21:44
  • How the hell did you got those imports inside your Activity class? Commented Oct 19, 2013 at 21:45
  • You forget the activity onCreate method Commented Oct 19, 2013 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 Commented Oct 19, 2013 at 22:13

5 Answers 5

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);
}
answered Oct 19, 2013 at 21:43
Sign up to request clarification or add additional context in comments.

6 Comments

@ramaral.. my bad that
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"
do u see R.java in your gen folder?
I mean gen\com\example\myandroid folder
All I can see there is BuildConfig.java, BuildConfig and DEBUG. I assume i'm looking in the right folder as this is the only "gen" I can seem to find... @GV
|
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

answered Oct 19, 2013 at 23:53

Comments

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.

answered Oct 19, 2013 at 22:09

1 Comment

Ended up with the same error on 2 more lines after doing this... @Nikoletta Muhari
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.

answered Oct 19, 2013 at 22:27

1 Comment

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
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);
}
answered Oct 19, 2013 at 23:03

8 Comments

Can your please take the screen shot of your erros if its too much for you to post errors here...
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 i.imgur.com/iWyt3jF.png?1?6503
have you defined your activity in AndroidManifest.xml. your second activity as well should be defined: <activity android:name=".DisplayMessageActivity" />. Right after your previous activity ends at.
Did not change anything... :/ @SHANKAR
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: <application ... > ... <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application>
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.