gpt4 book ai didi

java - 屏幕旋转时 fragment 崩溃

转载 作者:行者123 更新时间:2023-12-01 18:10:39 32 4
gpt4 key购买 nike

使用 FAB 按钮,应用程序在两个 fragment 之间交替。我试图在旋转屏幕时使当前 fragment 持续存在,但每次屏幕旋转时应用程序都会崩溃。我尝试了很多解决方案,但没有一个有效。为什么会崩溃?我正在使用 NetBeans,所以我不确定该由谁发布 logcat。

       public class agendaFalante extends AppCompatActivity
{
/** Called when the activity is first created. */
private Toolbar toolbar;
private FloatingActionButton fab ;
private listViewFrag lvfrag;
private newEventFrag nef;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toolbar = (Toolbar) findViewById (R.id.toolbar);
setSupportActionBar(toolbar);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
if (savedInstanceState == null) {
lvfrag = new listViewFrag();
fragmentTransaction.replace(R.id.ll, lvfrag, "lvfrag");
fragmentTransaction.commit();
}else{
if(lvfrag!=null && lvfrag.isVisible()){
fragmentTransaction.replace(R.id.ll, lvfrag);
fragmentTransaction.commit();
}else if(nef!=null && nef.isVisible()){
fragmentTransaction.replace(R.id.ll, nef);
fragmentTransaction.commit();
}
}
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fragmentTransaction = fragmentManager.beginTransaction();
if(nef!=null && nef.isVisible()){
lvfrag = (listViewFrag) getSupportFragmentManager().findFragmentByTag("lvfrag");
fragmentTransaction.replace(R.id.ll,lvfrag);
}else{
if(lvfrag.isVisible()){
nef = (newEventFrag) getSupportFragmentManager().findFragmentByTag("nef");
if(nef==null){
nef = new newEventFrag();
fragmentTransaction.replace(R.id.ll,nef,"nef");
}else{
fragmentTransaction.replace(R.id.ll,nef);
}
}
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
public boolean OnCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_layout,menu);
return true;
}

}

主xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root"
tools:context=".agendaFalante">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>

</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_done"
app:layout_anchor="@id/ll"
app:backgroundTint="#CE93D8"
app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>

fragment 1 xml

public class listViewFrag extends Fragment{

private ListView atividadesView;
private ArrayList<String> alist;
private ArrayAdapter<String> aDapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listviewfrag, container, false);
atividadesView = (ListView) view.findViewById(R.id.listView);
alist = new ArrayList<String>();
alist.add("oi");
aDapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,alist);
atividadesView.setAdapter(aDapter);
return view;
}
public void addText(String text){
alist.add(text);
aDapter.notifyDataSetChanged();
}
}

XML fragment :

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

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

fragment 2:

public class newEventFrag extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.neweventfrag, container, false);
return view;
}

}

XML fragment

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<TableRow
android:layout_height="0dp"
android:layout_weight="1">
<EditText
android:text="Insira o título"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
>
</EditText>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:id="@+id/record"
android:src="@drawable/ic_mic"
>
</Button>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1">
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false"
/>
</TableRow>
</TableLayout>

最佳答案

在您添加 fragment 的 Activity 的 AndroidManifest.xml 上写入此行::android:configChanges="orientation|screenSize|keyboardHidden"

<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

关于java - 屏幕旋转时 fragment 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269605/

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