我正在尝试创建像Fragment Factory这样的代码:
首先,我的活动onCreate方法调用“criarDashboard”方法,该方法负责创建包含仪表板的新片段..
public class DashboardActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Prancha prancha = criarPrancha();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, FragmentFactory.criarDashboard(this, prancha, false, false))
.commit();
}
public Prancha criarPrancha(){
byte[] img = new byte[0];
Item i1 = new ItemExpansivel("Eu quero", img, this);
Item i2 = new ItemExpansivel("Eu estou", img, this);
List<Item> itens = new ArrayList<Item>();
itens.add(i1);
itens.add(i2);
Prancha prancha = new Prancha(itens);
return prancha;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
}
这是FragmentFactory类:
public class FragmentFactory {
public static DashboardFragment criarDashboard(Activity context, Prancha prancha, boolean voltar, boolean proximo) {
DashboardFragment dashboardFragment = new DashboardFragment();
List<Item> itensHabilitados = separarItensHabilitados(prancha.getItens());
List<List<Item>> matrizItens = ordernarItems(itensHabilitados, voltar, proximo);
adicionarItemsLayour(matrizItens, dashboardFragment);
return dashboardFragment;
}
private static List<Item> separarItensHabilitados(List<Item> itens) {
List<Item> itensHabilitados = new ArrayList<Item>();
for(Item item : itens) {
if(item.isHabilitado()) {
itensHabilitados.add(item);
}
}
return itensHabilitados;
}
private static void adicionarItensNavegacao(final Activity context, boolean voltar, boolean proximo) {
if(voltar) {
Button btnBVoltar = new Button(context);
btnBVoltar.setText(R.string.voltar);
btnBVoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.onBackPressed();
}
});
}
if(proximo) {
Button btnBVoltar = new Button(context);
btnBVoltar.setText(R.string.proximo);
}
}
@SuppressWarnings("finally")
private static List<List<Item>> ordernarItems(List<Item> itens, boolean voltar, boolean proximo) {
int itensLayoutInferior = (voltar ? 1:0) + (proximo ? 1:0);
int i = 0;
List<Item> arraySuperior = new ArrayList<Item>();
List<Item> arrayInferior = new ArrayList<Item>();
try {
while (i < itensLayoutInferior) {
arraySuperior.add(itens.get(0));
itens.remove(0);
}
boolean inserirNoLayoutSuperior = true;
while (!itens.isEmpty()) {
if(inserirNoLayoutSuperior) {
arraySuperior.add(itens.get(0));
} else {
arrayInferior.add(itens.get(0));
}
itens.remove(0);
trocarValor(inserirNoLayoutSuperior);
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} finally {
List<List<Item>> matriz = new ArrayList<List<Item>>();
matriz.add(arraySuperior);
matriz.add(arraySuperior);
return matriz;
}
}
private static void trocarValor(boolean b) {
if(b) {
b = false;
} else {
b = true;
}
}
private static void adicionarItemsLayour(List<List<Item>> itens, DashboardFragment dashboardFragment) {
LinearLayout layoutSuperior =
(LinearLayout) dashboardFragment.getView().findViewById(R.id.linearLayoutSuperior);
LinearLayout layoutInferior =
(LinearLayout) dashboardFragment.getView().findViewById(R.id.linearLayoutInferior);
for (Item item : itens.get(0)) {
layoutSuperior.addView(item);
}
for (Item item : itens.get(1)) {
layoutInferior.addView(item);
}
}
}
adicionarItemsLayout方法中出现错误,它是dashboardFragment的getView方法中的一个简单空指针。 问题是..在这种情况下我应该如何实例化一个新的片段..