So I am following this tutorial, using Sublime as a text editor and compiling everything from console.
All was working good, but when we came to the part where you
It really helped much until I got a successful build. They also confused me with the MainActivity sometimes referring to MyActivity. So anyway the DisplayMessageActivity.java file looks as above and for a successful I had the MyActivity.java code as below:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends Activity {
//public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** 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);
}
}
And now I can run my apk :-)
If you use Eclipse to add in DisplayMessageActivity.java
then it derives from ActionBarActivity
and adds in all the support code related to that for you.
However if you are following the tutorial using commandline tools, then the ActionBarActivity
stuff is not set up yet. They get to that in the next part of the tutorial.
Instead, you can use the following code for DisplayMessageActivity.java
:
package com.example.yourname;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
protected 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);
setContentView(textView);
}
}
You will also need to add into MainActivity.java
when you are doing the step "Start the Second Activity":
import android.view.View;