Question

First time app developer and I'm trying to follow the lovely tutorial provided by the nice guys at Google.

Everything was going well until I got to the section on adding an ActionBar.

I tried following the instruction for supporting Android 2.1 and above. I went to the SDK manager, installed v7 appcompat, set up a new library project, imported appcompat, imported appcompat, set up the build paths, configured build paths, yadda yadda. Basically I followed these steps to the letter: http://developer.android.com/tools/support-library/setup.html#libs-with-res

Once that was done I adjusted MainActivity to extend ActionBarActivity and updated my manifest file to use Theme.AppCompat.Light for the theme. Continuing with the tutorial I added an ActionBar with a search and settings button, but upon trying to compile I ran into nothing but errors. Originally my rampant ctrl+shift+O'ing had imported android.R - a problem I found an fixed. However after removing that import statement and going to Project > Clean it isn't generating the R.java file. I've looked through every resource and there are no files with a capital letter in the name. There's also no errors in my XML files, and every reference to @string/<name> exists in the strings.xml file

Any advice on what to try next would be appreciated. I can also post the contents of any file that you suspect may be the culprit. I've spent around 4 hours trying to debug this myself but I don't know where to go next.

For @bluebrain:

Description Resource Location Type
Unable to resolve target 'android-16' android-support-v7-appcompat Uknown Android Target Problem

For @Gem_Ram:

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name="com.example.myfirstapp.MainActivity"
            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.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>

</manifest>

MainActivity.java (Every occurrence of R is giving me "R cannot be resolved to a variable"):

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                //openSearch();
                return true;
            case R.id.action_settings:
                //openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
        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);
    }

}

DisplayMessageActivity.java (same error):

package com.example.myfirstapp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

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

        // 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);

        // Set the text view as the activity layout
        setContentView(textView);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">My First App</string>
    <string name="action_search">Search</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="title_activity_display_message">My Message</string>

</resources>

Any other files you'd like to see?

Update:

After changing import android.support.v7.app.ActionBarActivity; to import android.support.v7.app.ActionBar; I got a huge collection of red squigglies that I could not resolve since all of the methods I implemented and called are derivatives of the Activity class. I changed it back to import android.support.v7.app.ActionBarActivity; and hit save, then my Problems tab went berserk. First there were 63 problems, then 1, then 7, then 3, then 18... Problems ranging from some process failing with a negative return code, to variables being undefined, to some dependency not existing,... In an attempt to make Eclipse stop having a seizure I clicked Project > Clean, and now the old problem ("Unable to resolve target 'android-16'") is gone, and replaced by 7 instances of the problem "R cannot be resolved to a variable"

Of course this is the problem I expected all along, since my R.java file isn't being generated.

Was it helpful?

Solution

I has the same issue as you mentioned here. I imported library adroid-support-v7-appcompat and error say:

Unable to resolve target 'android-16' android-support-v7-appcompat Uknown Android Target Problem

then I opened project.properties file in android-support-v7-appcompat project and edit line

target=android-19

to

target=android-16

I saved change and make change back to

target=android-19

I saved everything and project was recompiled and all errors disappeared. Now Im able to run project and everything look fine.

OTHER TIPS

Please share your code. I've run this app successfully without any issues. For your reference, adding my files here:

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.actionbar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!-- The main/home activity (it has no parent activity) -->
        <activity
            android:name="com.example.actionbar.HelloWorldMainActivity"
            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.actionbar.DisplayMessageActivity"
            android:label="@string/title_activity_display_message" 
            android:parentActivityName="com.example.actionbar.HelloWorldMainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.actionbar.HelloWorldMainActivity" />
        </activity>
    </application>

</manifest>

ActionBarActivity file:

package com.example.actionbar;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;


public class ActionBarMainActivity extends Activity {

    //public static final String EXTRA_MESSAGE = null;

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

    }
    @SuppressWarnings("unused")
    private void setUpActionBar() {
        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
}

MainActivity File:

package com.example.actionbar;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Intent;

public class HelloWorldMainActivity extends Activity {
    public static final String EXTRA_MESSAGE = "com.example.actionbar.MESSAGE";

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world_main);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            }
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
            }
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
            }
        };
        for ( int i = 0; i < 4; i++ ){
            actionBar.addTab(actionBar.newTab().setText("Tab" + (i+1)).setTabListener(tabListener));
        }
    }

    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);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_bar_main, menu);
        return super.onCreateOptionsMenu(menu); 
    }
    public boolean onOptionsItemsSelected(MenuItem item){
        switch (item.getItemId())
        {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        case R.id.action_call:
            openCall();
            return true;
        default: 
                return super.onOptionsItemSelected(item);
        }
    }

    private void openCall() {
        // TODO Auto-generated method stub

    }

    private void openSettings() {
        // TODO Auto-generated method stub

    }

    private void openSearch() {
        // TODO Auto-generated method stub

    }
}

Strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActionBar</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_search">Search</string>
    <string name="action_call">Call</string>
    <string name="title_activity_main">ActionBarMainActivity</string>
    <string name="title_activity_display_message">DisplayMessageActivity</string>

</resources>

Please update your "import android.support.v7.app.ActionBarActivity;" to

import android.support.v7.app.ActionBar; in your MainActivity.java as you are using the support library APIs

And I hope your AVD is properly configured to min-7 and max-19 as per your manifest description.

In your project.properties file, ensure it has ===> target=android-19

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top