按钮上的Android Studio更改片段单击

时间:2015-02-09 13:06:13

标签: java android android-fragments

基本上我有两个片段,每个片段有两个类,一个主类和主要活动。我想将其中一个片段设置为主屏幕,当我按下一个按钮时,它会显示另一个片段并隐藏前一个片段。

问题: 我的第一个问题是应用程序甚至没有打开,它说,"不幸的是应用程序停止工作"。 接下来,当我点击我的按钮时,两个片段都在合并,最后一个片段没有消失。

我的主要课程:

public class MainActivity extends ActionBarActivity {

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    UpcomingProject Upcoming = new UpcomingProject();
    fragmentTransaction.replace(android.R.id.content, Upcoming);
    fragmentTransaction.commit();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}



@Override
public void onBackPressed() {


}

我的主要活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".MainActivity">

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/upcomingproject"/>
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/createproject"/>




</LinearLayout>

我的主页片段xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
>

<Button
    android:id="@+id/button_create"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:text="@string/button_create"
    android:layout_margin="15sp"
    android:background="@drawable/drawable_buttoncreate"
    android:clickable="true" />




</LinearLayout>

我的主页片段java class:

public class UpcomingProject extends Fragment implements View.OnClickListener {

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView;
    rootView = inflater.inflate(R.layout.upcomingproject, container, false);
    Button CreateP = (Button)rootView.findViewById(R.id.button_create);
    CreateP.setOnClickListener(this);

    return rootView;
}
@Override
public void onClick(View v) {

    CreateProject Create = new CreateProject();
    fragmentTransaction.replace(android.R.id.content, Create);
    fragmentTransaction.commit();
}
}

我的第二个片段,即点击按钮我想切换的片段:

public class CreateProject extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
    View rootView;
    rootView = inflater.inflate(R.layout.createproject, container, false);

    return rootView;
}
}

第二个片段是xml:

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


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:scrollbarAlwaysDrawVerticalTrack="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <TextView android:id="@+id/name_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name_input"
            android:textSize="20sp"/>

        <EditText android:id="@+id/edit_message"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:hint="@string/edit_message"/>

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_marginBottom="10dp">

        <TextView android:id="@+id/contact_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contact_input"
            android:layout_gravity="top"
            android:textSize="20sp"/>

        <EditText android:id="@+id/contact_description"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:inputType="number"
            android:hint="@string/contact_description"/>

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_marginBottom="10dp">

        <TextView android:id="@+id/email_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/email_input"
            android:layout_gravity="top"
            android:textSize="20sp"/>

        <EditText android:id="@+id/email_description"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"


            android:inputType="textEmailAddress"
            />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_marginBottom="10dp"
        >

        <TextView android:id="@+id/category_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/category_input"
            android:layout_gravity="top"
            android:textSize="20sp"/>

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >


            <RadioButton android:id="@+id/radio_individual"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="@string/radio_individual"
                />

            <RadioButton android:id="@+id/radio_NPO"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="@string/radio_NPO"
                />

            <RadioButton android:id="@+id/radio_NGO"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="@string/radio_NGO"
                />

        </RadioGroup>

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_marginBottom="10dp"
        >

        <TextView android:id="@+id/title_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_input"
            android:layout_gravity="top"
            android:textSize="20sp"/>

        <EditText android:id="@+id/title_description"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:inputType="text" />

    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_marginBottom="10dp"
        >

        <TextView android:id="@+id/description_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/description_input"
            android:layout_gravity="top"
            android:textSize="20sp"/>

        <EditText android:id="@+id/edit_description"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:hint="@string/edit_description" />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:gravity="center"
        android:layout_marginBottom="10dp">

        <Button android:id="@+id/button_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_register"
            android:layout_margin="10sp"

            android:background="@drawable/drawable_buttoncreate"/>

        <Button android:id="@+id/button_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_reset"
            android:layout_margin="10sp"
            android:background="@drawable/drawable_buttoncreate"/>

    </LinearLayout>
</LinearLayout>

</ScrollView>

2 个答案:

答案 0 :(得分:0)

尝试在主要活动中使用getSupportFragmentManager()来替换第一个片段。当你替换第二个片段时,getChildFragmentManager()

答案 1 :(得分:0)

这是一个很晚的回复,但我会给出,以防其他人遇到同样的问题。 出现此问题的原因是您已在 xml 文件中明确定义了片段。如果您想交换片段,最好将其定义为 FrameLayout,然后使用例如

在您的 onCreate 方法中交换它
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
    FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
    fts.add(R.id.content, UpcomingProject.newInstance());
    fts.commit();
    setContentView(R.layout.activity_main);
}
This is all explained very well and in much more detail in the link in hrskrs's comment above (https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments)