gpt4 book ai didi

java - Android Studio 在按钮单击时更改 fragment

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:17 26 4
gpt4 key购买 nike

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

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

我的主要类(class):

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() {


}

我的主要 Activity :

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

我的主页 fragment 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>

我的主页 fragment java类:

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();
}
}

我的第二个 fragment ,我也想通过单击按钮来切换:

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;
}
}

第二个 fragment 的 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>

最佳答案

尝试在主 Activity 中使用 getSupportFragmentManager() 来替换第一个 fragment 。当您替换第二个 fragment 时 getChildFragmentManager()

关于java - Android Studio 在按钮单击时更改 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com