В принципе у меня есть два фрагмента с двумя классами для каждого и одного основного класса и основного вида деятельности. Я хочу установить один из фрагментов в качестве главного экрана, и когда я нажимаю кнопку, он показывает другой фрагмент и скрывает предыдущий.Фрагмент изменения студии Android на кнопке Нажмите
Проблемы: Моя первая проблема заключается в том, что приложение даже не открывается, его высказывание «К сожалению приложение перестало работать». Далее, когда я нажимаю на свою кнопку, оба фрагмента объединяются, и последний фрагмент не исчезает.
Мой главный класс:
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 класс:
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>
обеспечивают журнал пожалуйста – mcd
читать о фрагментах [здесь] (https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments), и вы будете придумать решение – hrskrs
попробуйте .add вместо .replace и фрагменты выиграли t merge –