1

All the beginner tutorials I have seen use activity_main.xml for designing the layout and there is no fragment_main.xml. However, whenever I am doing it, I have two xml layouts generated by eclipse, an activity_main and a fragment_main. Activity_main is blank while fragment_main contains the hello world. I dragged in some elements and changed the line setContentView(R.layout.activity_main); to setContentView(R.layout.fragment_main);. The errors disappeared but the app crashes on my device on opening.

I am unable to follow any of the tutorials because of this. Any idea what is going on?

The autogenerated fragment_main.xml

<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.sampel.MainActivity$PlaceholderFragment" >

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

</RelativeLayout>

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="app_name">testApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    </resources>
yayu
  • 7,758
  • 17
  • 54
  • 86
  • U must be using fragments, Fragments requires a container to hold them. They cannot lie on their own.Since u have not shared the code of `fragment_main` , i can only help u this much – Deb Sep 08 '14 at 13:43
  • 1
    Check out my example project setup for using Fragments here: http://codereview.stackexchange.com/questions/57543/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default – EpicPandaForce Sep 08 '14 at 13:52
  • Can u post the `OnCreate()` and `activity_main`? Or u can follow the link provided by @Zhuinden – Deb Sep 08 '14 at 14:01
  • By the way, if you want to figure out why something doesn't work, you should look at the Logcat: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this (you can find the logcat in Window -> Show View if I remember correctly) – EpicPandaForce Sep 08 '14 at 14:21

1 Answers1

2

In order to make "hello world" work with only Activity, the setup is the following:

res/layout/activity_main.xml:

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

<TextView
    android:centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java:

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super(saveInstanceState);
        setContentView(R.layout.activity_main);
    }
}

AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="wholepackagename.activity.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>
    </application>
</manifest>

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Application name</string>
    <string name="hello_world">Hello world!</string>
</resources>
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I will have to get back to your answer (and the other one you linked). Meanwhile, is there any way to downgrade to an earlier version where I don't have to deal with fragments at this stage? They weren't appearing earlier and the MainActivity.java was a much simpler class for beginners to understand and start working with. – yayu Sep 08 '14 at 14:21
  • I don't think so, but you basically just have to remove the `PlaceholderFragment` class and the `fragment_main.xml` completely, and replace the content of `activity_main.xml` with something that can function on its own, such as a `LinearLayout` and a view hierarchy (such as a single `TextView`). – EpicPandaForce Sep 08 '14 at 14:23
  • I removed the class, the fragment main, and placed a RelativeLayout on activity_main.xml with a plain text. There is an error in the line, `if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } }` should I delete it? – yayu Sep 08 '14 at 14:29
  • you should keep only what I left here in my activity code – EpicPandaForce Sep 08 '14 at 14:29
  • 1
    So, yes. Also, `Fragments` aren't as complicated as they look, they're basically almost the same as the `Activity` except instead of using `onCreate(..) { super(..); setContentView(R.layout.main); }`, you use `onCreateView(..) { return inflater.inflate(R.layout.main, container, false); }`. – EpicPandaForce Sep 08 '14 at 14:43