我正在设计一个警报应用程序。
我正在使用一个充当我的闹钟的片段,想法是为每个新闹钟使用相同的片段(我不知道这是否正确)。
那么,我基本上试图添加,通过按下"新警报"按钮,同样的片段再次进入活动的布局,但当我按下按钮时,应用程序崩溃。有什么帮助吗?
我将片段插入到一个位于RelativeLayout内部的ScrollView内的LinearLayout
package com.example.roo.proyi;
import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class alarms extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarms);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
alarm_fragment alarmFragment = new alarm_fragment();
fragmentTransaction.add(R.id.alarmListContainer,alarmFragment);
fragmentTransaction.commit();
Button button_newAlarm = (Button)findViewById(R.id.button_new_alarm);
button_newAlarm.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
alarm_fragment alarmFragment2 = new alarm_fragment();
fragmentTransaction.add(R.id.alarmListContainer,alarmFragment2);
fragmentTransaction.commit();
}
;
}
);
.
.
.
.
.
.CONTINUES BUT IT IS IRRELEVANT
.
.
.
通过创建不同的碎片转移制作的最终解决方案
public class alarms extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarms);
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final alarm_fragment alarmFragment = new alarm_fragment();
fragmentTransaction.add(R.id.alarmListContainer,alarmFragment);
fragmentTransaction.commit();
Button button_newAlarm = (Button)findViewById(R.id.button_new_alarm);
button_newAlarm.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
android.support.v4.app.FragmentTransaction fragmentTransaction2 = fragmentManager.beginTransaction();
getSupportFragmentManager().beginTransaction();
alarm_fragment alarmFragment2 = new alarm_fragment();
fragmentTransaction2.add(R.id.alarmListContainer,alarmFragment2);
fragmentTransaction2.commit();
}
;
}
);
答案 0 :(得分:0)
您无法使用相同的FragmentTransaction
在OnClickListener
内添加片段。在交易中致电commit()
后,您无法再次使用它。致电getSupportFragmentManager().beginTransaction()
以获得新交易。
答案 1 :(得分:0)
检查此代码 -
1。它会检查您是否已在堆栈中添加了对话框片段。如果是,它将删除片段并使用新的argument
添加新片段,如果它不存在,它仍会添加片段。
看看你是否可以在代码中加入这种方法 -
void showDialog() {
mStackLevel++;
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}