Saturday, August 11, 2012

Missing Info from Android Building Your First App Tutorial

The android tutorial for "Build Your First App" (http://developer.android.com/training/basics/firstapp/index.html) is excellent, but they leave a couple of things out that I had to figure out for myself.  I am developing on ubuntu linux and following the command line version of the tut.  When I got to this part of the tut:

http://developer.android.com/training/basics/firstapp/running-app.html

I ran into the error "could not find javac" when I ran "ant debug".
I had to install javac like so ...

$ sudo apt-get install openjdk-6-jdk

The next problem was that I kept getting ...


error: device not found
- waiting for device -

... when I executed:



red@ubuntu:~/android/workspace/MyFirstApp$ adb install bin/MyFirstApp-debug.apk


... I found out that I need to start the "server" like so ....


red@ubuntu:~/android/workspace/MyFirstApp$ adb kill-server
red@ubuntu:~/android/workspace/MyFirstApp$ adb start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
red@ubuntu:~/android/workspace/MyFirstApp$ adb install bin/MyFirstApp-debug.apk
100 KB/s (37439 bytes in 0.363s)
pkg: /data/local/tmp/MyFirstApp-debug.apk
Success



4 comments:

  1. Later on in the tut (http://developer.android.com/training/basics/firstapp/starting-activity.html)

    They forget to tell you add this line ...

    Display Message



    ... to ...
    ~/android/workspace/MyFirstApp/res/values/strings.xml

    If you do not do this you will get the error message ...


    [aapt] /home/red/android/workspace/MyFirstApp/bin/AndroidManifest.xml:14: error: Error: No resource found that matches the given name (at 'label' with value '@string/title_activity_display_message').

    ... when you execute ...

    ant debug

    ReplyDelete
    Replies
    1. Dang ... I posted xml and forgot to escape the < chars

      Delete
  2. If you are getting those ...

    [javac] Compiling 1 source file to /home/red/android/workspace/MyFirstApp/bin/classes
    [javac] /home/red/android/workspace/MyFirstApp/src/com/example/myfirstapp/DisplayMessageActivity.java:9: cannot find symbol
    [javac] symbol: class Activity
    [javac] public class DisplayMessageActivity extends Activity {
    [javac]

    ... error messages you need to have your java files look like this ...


    red@ubuntu:~/android/workspace/MyFirstApp/src/com/example/myfirstapp$ cat MainActivity.java
    package com.example.myfirstapp;

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

    public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.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) {
    // Do something in response to button
    Intent intent = new Intent(this, com.example.myfirstapp.DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    }
    }


    red@ubuntu:~/android/workspace/MyFirstApp/src/com/example/myfirstapp$ cat DisplayMessageActivity.java
    package com.example.myfirstapp;
    import android.app.Activity;
    import android.content.Intent;
    import android.widget.TextView;
    import android.view.View;
    import android.os.Bundle;


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

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

    ReplyDelete
  3. included all import statements as u mentioned and it worked like charm !!!

    ReplyDelete