Google Android Tutorial - Not Compiling

I did everything as stated in this tutorial: Basic Google Android Tutorial and despite everything done as described, the code refuses to compile with 3 errors. Looks like the guys writing the tourizer forgot to mention what they are and where / how I define them.

The errors I am getting:

Error:(24, 68) error: cannot find symbol variable container
Error:(36, 23) error: cannot find symbol variable action_settings
Error:(46, 54) error: cannot find symbol variable fragment_display_message

      

None of the three fields are defined anywhere (perhaps one of the libraries is wrong?) This file:

package com.example.asteroth.first;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.R;

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
        setContentView(textView);
//        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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    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;
        }
    }
}

      

I'm using Android Studio, which I just downloaded, and no question from a search or similar questions points to an issue like this, so I suspect the authors of the tutorial forgot to mention something minor. I've seen a suggestion to put "container" as a new identifier in one of the XML files, to no avail.

EDIT: 'Can't find ActionBarActivity symbol' following Android Development Tutorial? This article proposes a solution, however it changes the ActionBarActivity to a simple activity, which is very different from what the tutorial is using, and I don't know how severe the impact it will have.

EDIT2: Problems found and removed: import android.R // throws error action_settings Invalid container // should have added it to xml file as id xml file named wrong // If I got it right, I'm still waiting for someone then experienced this, but it looks like the tutorial used a different name for the xml file and then the one that links to the java code

Remaining problem is similar to this Cannot resolve the placeplacefragment method error however I both extend the fragment and include android.app.Fragment as you can see in the included file.

+3


source to share


6 answers


I tried the same tutorial and this is how I fixed my mistakes:

Rid.container error not resolved

I had to import the android.support.v4.app.Fragment file to fix this issue and add android: id = "@ + id / container" to the RelativeLayout section in the activity_display_message.xml file.

fragment_display_message cannot be resolved error

Change R.layout.fragment_display_message to R.layout.activity_display_message instead. There is no need to create a new xml file for month_size_fragmentation.



This should fix these two errors.

But you would probably be better off if you comment out the if (savedInstanceState ............) statement, as otherwise your program will crash as soon as you try to run it if it doesn't give you any errors.

Your onCreate method should look like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_display_message);
    Intent intent=getIntent();
    String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView textView=new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    setContentView(textView);

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

      

+5


source


I am doing the tutorial for the first time on Feb 23, 2015 and ran into this compilation error, although it seems to me that I followed these steps closely. I changed the fragment_display_message file to activity_display_message, which is the XML file they create in the tutorial. This seems to fix the error and allow the application to run.



// 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.activity_display_message,
                container, false);
        return rootView;
    }
}

      

+2


source


This is a copy error. If you paste the code with "R." in it the development environment always imports android.R:

import android.R;

      

If you use R.id .... it is always looking for android .R, not its own R class. Remove the imports and you should be fine. This common work works for me.

After that, you should check if you have already defined the id and layout. You can check the layouts by looking package explorer

under res->layout

. In your example, it should be fragment_display_message.xml

.

For id's

you need to find all your layouts and check if there are specific views like container

.

0


source


Add this line to take care of your first error: android:id = "@+id/container"

You are getting this error because it is container

not in XML.

Add <string name="action_settings">Action Settings</string>

to "Action settings" which I assume does not exist in your XML as you have this error.

Create your own XML file with this exact name fragment_display_message.xml

to handle this error and check what code you might need to insert into your google tutorial. Often with Eclipse these files are not included for reasons beyond my knowledge. Therefore, you must create them or insert them yourself. (Make sure you have the latest SDK installed.

EDIT: Make sure the correct import matches your "tutorial". I took over the gander and saw that you were missing two imports. One of which answered another user.

0


source


I got a similar error in the Build Simple User Interface step:

Error:(18, 54) error: cannot find symbol variable toolbar

      

I have narrowed down the reason to res / layout / activity_my.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

      

The original version that compiled (but not a button or textbox):

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

      

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MyActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_my" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

      

0


source


fragment_display_message Make sure you have a file called fragment_display_message.xml in your res / layout folder.

action_settings Make sure you have this element in your menu.xml file in res / menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=
           ".MenuExampleActivity" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="1"
        app:showAsAction="never"
        android:title="My menu option"/>
</menu>

      

container Make sure you have a layout (eg RelativeLayout) with id set to "container" in your activity_main.xml file in res / layout, given that this is a link to the code to insert the snippet there.

0


source







All Articles