У меня есть две отдельные кнопки, которые отображают отдельные фрагменты при щелчке. Это мой xml-файл.Confused from andriod.app.Fragment и layout.Fragment
<?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:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.sjha.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_one"
android:text="Fragment1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_two"
android:text="Fragment2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:name="layout.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment">
</fragment>
</LinearLayout>
</LinearLayout>
я потерял где-то между android.app.Fragment
и layout.Fragment
.Please помочь мне исправить это.
Это мой java-файл.
package com.example.sjha.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import layout.FragmentOne;
import layout.FragmentTwo;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeFragment(View v)
{
Fragment fragment;
if (v==findViewById(R.id.fragment_one))
{
//HERE IS THE ERROR..
//I AM LOST BETWEEN android.app.Fragment and layout.Fragment
//FragmentTwo() is layout.Fragment while fragment is android.app.Fragment
fragment= new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment, fragment);
ft.commit();
}
}
}
Как исправить это?