user3020345 user3020345 - 4 months ago 20
Android Question

error while developing a simple android app

I am trying to create a simple android app. I followed the following link. https://developer.android.com/training/basics/firstapp/building-ui.html.

In the developers website they asked me to edit activity_main.xml.in which they asked to delete the text view and change the relative layout to linear layout. To my surprise, my activity_main.xml was different from the developer and i edited it with exactly wat they mentioned in the tutorial. but now when i went through the project folder i found another xml file which is named fragment_main.xml(resembled developers activity_main.xml) and this xml contained tags like relative layout and textview which was not there in my activity_main.xml. I do not know how and why this happend. I am confused whether to edit my fragment_main.xml just the way i did for activity_main.xml?

MainActivity.java

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

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


Now i am getting error in my Mainactivity.java.

the error says that the container cannot be resolved or is not a field.

This is how my activity_main.xml looks.

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


<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"/>

</LinearLayout>


this is how my fragment_main.xml looks.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.MainActivity$PlaceholderFragment" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/edit_message" />




Any help would be appreciated.

Answer

I've seen this tutorial before for a similar question - the answer is to include this attribute for the LinearLayout in activity_main.xml.

android:id="@+id/container"

If I remember correctly, your fragment_main.xml is for an activity that extends FragmentActivity which comes later.

Comments