Wednesday, 4 February 2015

Android Hello World Tutorial stops when tested



I am up to the "Start Another Activity" section of the Android tutorial and it simply won't work when I install and test it.


It compiles fine but running it breaks after I click the send button.


I am using the command line tools on Ubuntu 12.04 and installing to a real device, my Galaxy S5.


I am aware of logcat but haven't been able to get it working, it either shows no output at all, or gives a massive spam of output I can't keep up with. I would happily provide logcat information if I could manage to isolate my app's output and cut through everything else.


I have seen a lot of similar questions on this which leads me to believe the tutorial isn't very well written.


Here is "MyActivity.java"



package com.example.myfirstapp;

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

public class MyActivity extends Activity
{
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);
}
}


Here is DisplayMessageActivity



package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.*;
import android.view.*;
import android.widget.*;
import com.example.myfirstapp.R;

public class DisplayMessageActivity extends ActionBarActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

//Get the message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.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);
}
}


Here is AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0">

<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

<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="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MyActivity">

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MyActivity"
/>
</activity>

</application>
</manifest>


There was some code that I changed and took out as it said to add code in, but then would later show a "this is what your code should look like" and it was missing some of the original code. I also changed the manifest package names in the activities because at the beginning it uses com.example.myfirstapp but later uses com.mycompany.myfirstapp


Any help or advice on why this seemingly simple tutorial doesn't work is greatly appreciated.


No comments:

Post a Comment