2

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 are supposed to make second Activity. They used Eclipse to autogenerate it and told me to paste it down, so I did.

Here is my code (I added some imports and package at the beginning, it fixes some problems)

/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java

package pl.qnsi.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.MenuItem;


public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_display_message,
                      container, false);
              return rootView;
        }
    }
}

And an error log from console

[qnsi@archie MyFirstApp]$ ant debug
Buildfile: /home/qnsi/code/Apps/learning/MyFirstApp/build.xml

-set-mode-check:

-set-debug-files:

-check-env:
 [checkenv] Android SDK Tools Revision 22.6.4
 [checkenv] Installed at /opt/android-sdk

-setup:
     [echo] Project Name: MyFirstApp
  [gettype] Project Type: Application

-set-debug-mode:

-debug-obfuscation-check:

-pre-build:

-build-setup:
[getbuildtools] Using latest Build Tools: 19.1.0
     [echo] Resolving Build Target for MyFirstApp...
[gettarget] Project Target:   Android 4.4.2
[gettarget] API level:        19
     [echo] ----------
     [echo] Creating output directories if needed...
     [echo] ----------
     [echo] Resolving Dependencies for MyFirstApp...
[dependency] Library dependencies:
[dependency] No Libraries
[dependency] 
[dependency] ------------------
     [echo] ----------
     [echo] Building Libraries with 'debug'...
   [subant] No sub-builds to iterate on

-code-gen:
[mergemanifest] No changes in the AndroidManifest files.
     [echo] Handling aidl files...
     [aidl] No AIDL files to compile.
     [echo] ----------
     [echo] Handling RenderScript files...
     [echo] ----------
     [echo] Handling Resources...
     [aapt] No changed resources. R.java and Manifest.java untouched.
     [echo] ----------
     [echo] Handling BuildConfig class...
[buildconfig] No need to generate new BuildConfig.

-pre-compile:

-compile:
    [javac] Compiling 3 source files to /home/qnsi/code/Apps/learning/MyFirstApp/bin/classes
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:12: error: cannot find symbol
    [javac] public class DisplayMessageActivity extends ActionBarActivity {
    [javac]                                             ^
    [javac]   symbol: class ActionBarActivity
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:40: error: cannot find symbol
    [javac]     public static class PlaceholderFragment extends Fragment {
    [javac]                                                     ^
    [javac]   symbol:   class Fragment
    [javac]   location: class DisplayMessageActivity
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:45: error: cannot find symbol
    [javac]         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    [javac]                                  ^
    [javac]   symbol:   class LayoutInflater
    [javac]   location: class PlaceholderFragment
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:45: error: cannot find symbol
    [javac]         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    [javac]                                                           ^
    [javac]   symbol:   class ViewGroup
    [javac]   location: class PlaceholderFragment
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:45: error: cannot find symbol
    [javac]         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    [javac]                ^
    [javac]   symbol:   class View
    [javac]   location: class PlaceholderFragment
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:16: error: cannot find symbol
    [javac]         super.onCreate(savedInstanceState);
    [javac]         ^
    [javac]   symbol:   variable super
    [javac]   location: class DisplayMessageActivity
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:17: error: cannot find symbol
    [javac]         setContentView(R.layout.activity_display_message);
    [javac]                                ^
    [javac]   symbol:   variable activity_display_message
    [javac]   location: class layout
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:21: error: cannot find symbol
    [javac]                 .add(R.id.container, new PlaceholderFragment()).commit();
    [javac]                          ^
    [javac]   symbol:   variable container
    [javac]   location: class id
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:20: error: cannot find symbol
    [javac]             getSupportFragmentManager().beginTransaction()
    [javac]             ^
    [javac]   symbol:   method getSupportFragmentManager()
    [javac]   location: class DisplayMessageActivity
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:14: error: method does not override or implement a method from a supertype
    [javac]     @Override
    [javac]     ^
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:31: error: cannot find symbol
    [javac]         if (id == R.id.action_settings) {
    [javac]                       ^
    [javac]   symbol:   variable action_settings
    [javac]   location: class id
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:34: error: cannot find symbol
    [javac]         return super.onOptionsItemSelected(item);
    [javac]                ^
    [javac]   symbol:   variable super
    [javac]   location: class DisplayMessageActivity
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:25: error: method does not override or implement a method from a supertype
    [javac]     @Override
    [javac]     ^
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:47: error: cannot find symbol
    [javac]               View rootView = inflater.inflate(R.layout.fragment_display_message,
    [javac]               ^
    [javac]   symbol:   class View
    [javac]   location: class PlaceholderFragment
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:47: error: cannot find symbol
    [javac]               View rootView = inflater.inflate(R.layout.fragment_display_message,
    [javac]                                                        ^
    [javac]   symbol:   variable fragment_display_message
    [javac]   location: class layout
    [javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:44: error: method does not override or implement a method from a supertype
    [javac]         @Override
    [javac]         ^
    [javac] 16 errors

BUILD FAILED
/opt/android-sdk/tools/ant/build.xml:720: The following error occurred while executing this line:
/opt/android-sdk/tools/ant/build.xml:734: Compile failed; see the compiler error output for details.

Total time: 1 second

I googled for some answers but most of the errors were fixed by importing the libraries I already imported. I never saw a problem with overloading method or not finding super method. I am clueless what to do.

M.M
  • 138,810
  • 21
  • 208
  • 365
qnsi
  • 370
  • 4
  • 14
  • 2
    There seem to be some more imports that you're missing. Have you thought of using an IDE such as Eclipse, which can help you find them? – Dawood ibn Kareem Jun 24 '14 at 09:29
  • The first error. You need to import ActionBarActivity – Terry Jun 24 '14 at 09:29
  • I think that if you add `import android.support.v7.app.ActionBarActivity;` then many of these errors will disappear. But not all of them, unfortunately. – Dawood ibn Kareem Jun 24 '14 at 09:31
  • 2
    Please use an IDE (preferably eclipse) for developing android apps. You will be wasting a hell lot of time trying to figure out errors if you don't use an IDE.. You might end up with errors which are more complex than missing import statements.. :) – TheLostMind Jun 24 '14 at 09:31
  • Better use IDE. Here the link download the adt bundle http://developer.android.com/sdk/index.html#ExistingIDE – gjman2 Jun 24 '14 at 09:38
  • For Android development I suggest IntelliJ because is has better Android features than Eclipse. Google offers a bundled IntelliJ 'Android Studio': https://developer.android.com/sdk/installing/studio.html – Adriaan Koster Jun 24 '14 at 09:44
  • @DavidWallace I just did, and it fixes some problems (now I have 10 errors). EDIT: adding `import android.support.v7.app.ActionBarActivity;` as fixed all the following errors. Feel free to post it as separate answer. – qnsi Jun 24 '14 at 09:53
  • Seriously, LittleMary, get yourself a proper IDE. I am not going to find all the required imports for you. – Dawood ibn Kareem Jun 24 '14 at 10:04
  • @DavidWallace yes, I just realized the importance of IDE and am downloading Android Studio. Your 2 comments fixed all errors. – qnsi Jun 24 '14 at 10:06
  • @LittleMary if you have solved the problem, could you post as an answer the the list of includes which is working – M.M Oct 27 '14 at 04:50
  • I love ninjas. I send you my bows. I bet you have all the time in the world. – Nikola Despotoski Oct 27 '14 at 05:15

2 Answers2

0

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;
M.M
  • 138,810
  • 21
  • 208
  • 365
0

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 :-)

Alex K
  • 8,269
  • 9
  • 39
  • 57