Reputation: 8098
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>
Upvotes: 1
Views: 548
Reputation: 81568
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>
Upvotes: 2