"Can't find symbol ActionBarActivity" by following Android development tutorial?

So, I am following this tutorial using Sublime as my text editor and compiling everything from the console.

Everything worked well, but when we got to the part where you have to do the second activity. They used Eclipse to autogenerate and told me to insert it, so I did.

Here is my code (I added some imports and a 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 the error log from the 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 searched for some answers, but most of the errors were fixed by importing the libraries I already imported. I've never seen an issue with the overload method or found a super method. I do not know what to do.

+2


source to share


2 answers


If you use Eclipse to add to DisplayMessageActivity.java

, then it comes from ActionBarActivity

and adds to all the support code associated with it for you.

However, if you are following the tutorial using command line tools, the material ActionBarActivity

is not set up yet. They get to that in the next part of the tutorial.

Instead, you can use the following code to 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 also need to add in MainActivity.java

when you do the Start Second Action step:

import android.view.View;

      

0


source


It helped a lot until I got a successful build. They also confused me with MainActivity, sometimes referring to MyActivity. Anyway the DisplayMessageActivity.java file looks like above and for success 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 apk :-)

0


source







All Articles