Search code examples
javaandroidr.java-file

First time app developer, attempting to add ActionBar broke everything


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.


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.