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.
5 Answers 5
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);
}
6 Comments
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
Comments
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.
1 Comment
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.
1 Comment
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);
}
onCreatemethod