Android fragment_main.xml

Android fragment_main.xml,android,Android,我看到的所有初学者教程都使用activity_main.xml来设计布局,并且没有片段_main.xml。然而,每当我这样做的时候,我都有两个由eclipse生成的xml布局,一个activity_main和一个fragment_main。Activity_main为空,而fragment_main包含hello world。我拖入了一些元素并更改了行setContentView(R.layout.activity\u main)到setContentView(R.layout.fragment

我看到的所有初学者教程都使用activity_main.xml来设计布局,并且没有片段_main.xml。然而,每当我这样做的时候,我都有两个由eclipse生成的xml布局,一个activity_main和一个fragment_main。Activity_main为空,而fragment_main包含hello world。我拖入了一些元素并更改了行
setContentView(R.layout.activity\u main)
setContentView(R.layout.fragment\u main)。错误消失了,但应用程序在打开时在我的设备上崩溃

由于这个原因,我无法学习任何教程。知道发生了什么吗

自动生成的片段_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>

特斯塔普
你好,世界!
设置
为了使“hello world”仅适用于活动,设置如下:

res/layout/activity\u 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>
public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super(saveInstanceState);
        setContentView(R.layout.activity_main);
    }
}
<?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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Application name</string>
    <string name="hello_world">Hello world!</string>
</resources>
AndroidManifest.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>
public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super(saveInstanceState);
        setContentView(R.layout.activity_main);
    }
}
<?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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Application name</string>
    <string name="hello_world">Hello world!</string>
</resources>

res/values/strings.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>
public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super(saveInstanceState);
        setContentView(R.layout.activity_main);
    }
}
<?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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Application name</string>
    <string name="hello_world">Hello world!</string>
</resources>

应用程序名称
你好,世界!

U必须使用碎片,碎片需要一个容器来容纳它们。他们不能靠自己撒谎。既然你没有分享
fragment\u main
的代码,我只能在这方面帮助你much@Deb添加到问题中。在这里查看我使用片段的示例项目设置:您可以发布
OnCreate()
活动\u main
?或者你可以按照@ZhuindenBy提供的链接,如果你想知道为什么有些东西不起作用,你应该看看日志:(如果我没记错的话,你可以在Window->Show View中找到日志),我必须回到你的答案(以及你链接的另一个)。同时,有没有办法降级到早期版本,在这个阶段我不必处理片段?它们没有出现得更早,MainActivity.java对于初学者来说是一个简单得多的类,可以让他们理解并开始使用。我不这么认为,但基本上你只需要完全删除
placeholder fragment
类和
fragment\u main.xml
,并将
activity_main.xml
的内容替换为可以独立运行的内容,例如
LinearLayout
和视图层次结构(例如单个
TextView
)。我删除了类fragment main,并在activity_main.xml上放置了一个带有纯文本的RelativeLayout。行中有一个错误,
if(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.container,new Placeholder Fragment()).commit();}}}
我应该删除它吗?你应该只保留我在活动代码中留下的内容,所以,是的。另外,
片段不像看上去那么复杂,它们基本上与
活动
相同,只是没有使用
onCreate(..){super(..);setContentView(R.layout.main);}
,而是使用
onCreateView(..){return inflater.inflate(R.layout.main,container,false);}