我有一个简单的listFragment:
public class ListUtente extends ListFragment{
OnDetailUtenteSelezionato mCallback;
private String[] utenti = {"Aldo","Giovanni","Giacomo"};
ArrayAdapter<String> adapter;
public interface OnDetailUtenteSelezionato {
public void ondetailUtenteSelezionato(int position);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
// set adapter
setListaUtenti();
setHasOptionsMenu(true);
}
private void setListaUtenti(){
if(getListAdapter()==null){
// creo l'adapter
adapter=new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
utenti);
setListAdapter(adapter);
}
else
{
adapter.notifyDataSetChanged();
}
}
@Override
public void onDestroyView(){
super.onDestroyView();
setListAdapter(null);
}
@Override
public void onStart() {
super.onStart();
if (getFragmentManager().findFragmentById(R.id.detailutente_fragment) != null) {
getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnDetailUtenteSelezionato) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " devi implementare OnDetailUtenteSelezionato");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.ondetailUtenteSelezionato(position);
// Set the item as checked to be highlighted when in two-pane layout
//getListView().setItemChecked(position, true);
}
}
片段细节:
public class DettUtente extends Fragment{
final static String ARG_UTENTE = "utente";
int mUtenteCorrente = 0;
// onCreateView
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
if (savedInstanceState != null) {
mUtenteCorrente = savedInstanceState.getInt(ARG_UTENTE);
}
return inflater.inflate(R.layout.utente_detail,container,false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateUtenteView(args.getInt(ARG_UTENTE));
}
updateUtenteView(mUtenteCorrente);
}
public void updateUtenteView(int position) {
TextView nome = (TextView) getActivity().findViewById(R.id.detailName);
nome.setText("fabio");
mUtenteCorrente = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(ARG_UTENTE, mUtenteCorrente);
}
}
我的活动:
public class ActivityUtente extends FragmentActivity implements ListUtente.OnDetailUtenteSelezionato{
private boolean mDualFragments = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_utente);
if(findViewById(R.id.framelayout2) != null){
mDualFragments=true;
ListUtente lu = new ListUtente();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.framelayout1, lu);
transaction.commit();
//nella seconda schermata inserisco il dettaglio
DettUtente du = new DettUtente();
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
transaction1.add(R.id.framelayout2, du);
transaction1.commit();
}
else
{
ListUtente lu = new ListUtente();
//lu.setArguments(0/*getIntent().getExtras()*/);
getSupportFragmentManager().beginTransaction()
.add(R.id.framelayout1, lu).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.activity_utente, menu);
return true;
}
@Override
public void ondetailUtenteSelezionato(int position) {
DettUtente dettUtente;
if (mDualFragments) {
dettUtente = (DettUtente)
getSupportFragmentManager().findFragmentById(R.id.detailutente_fragment);
if (dettUtente != null && dettUtente.isVisible()) {
dettUtente.updateUtenteView(position);
}
}
else{
dettUtente = new DettUtente();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.framelayout1, dettUtente);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
我的问题是当我旋转设备时。我想改变我的观点(从垂直到水平等)但是从水平到垂直我有这个错误:
02-27 15:31:34.128: E/AndroidRuntime(10786): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unipg.utente/com.unipg.utente.ActivityUtente}: java.lang.NullPointerException
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2212)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3640)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread.access$700(ActivityThread.java:144)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.os.Looper.loop(Looper.java:137)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread.main(ActivityThread.java:4966)
02-27 15:31:34.128: E/AndroidRuntime(10786): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 15:31:34.128: E/AndroidRuntime(10786): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 15:31:34.128: E/AndroidRuntime(10786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-27 15:31:34.128: E/AndroidRuntime(10786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-27 15:31:34.128: E/AndroidRuntime(10786): at dalvik.system.NativeStart.main(Native Method)
02-27 15:31:34.128: E/AndroidRuntime(10786): Caused by: java.lang.NullPointerException
02-27 15:31:34.128: E/AndroidRuntime(10786): at com.unipg.utente.DettUtente.updateUtenteView(DettUtente.java:50)
02-27 15:31:34.128: E/AndroidRuntime(10786): at com.unipg.utente.DettUtente.onStart(DettUtente.java:45)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1866)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.Activity.performStart(Activity.java:5018)
02-27 15:31:34.128: E/AndroidRuntime(10786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
02-27 15:31:34.128: E/AndroidRuntime(10786): ... 12 more
02-27 15:31:34.148: W/ActivityManager(278): Force finishing activity com.unipg.utente/.ActivityUtente