Impossibile riuscire a utilizzare le schede con Sherlock
-
11-12-2019 - |
Domanda
OK, quindi sto cercando di usare Sherlock per visualizzare più schede, ciascuna per un frammento. Ho solo 4 classe: una per la mia attività principale, due per i miei frammenti, e uno per il tablenener. Tutto dovrebbe essere OK (Ho lo stesso programma senza Sherlcock, lavorando su 4.0 Dispositivi), quindi non riesco a capire perché ottengo quella NullPointerException.
Ecco parte dell'errore
05-18 17:46:57.197: E/AndroidRuntime(9312): FATAL EXCEPTION: main
05-18 17:46:57.197: E/AndroidRuntime(9312): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micky.testing/com.micky.testing.SherlockTestActivity}: java.lang.NullPointerException
...
05-18 17:46:57.197: E/AndroidRuntime(9312): Caused by: java.lang.NullPointerException
05-18 17:46:57.197: E/AndroidRuntime(9312): at com.micky.testing.MyTabListener.onTabSelected(MyTabListener.java:21)
...
05-18 17:46:57.197: E/AndroidRuntime(9312): at com.micky.testing.SherlockTestActivity.onCreate(SherlockTestActivity.java:39)
.
Ecco uno dei miei frammenti:
HomeFragment
.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
public class HomeFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.homefragment, container, false);
}
}
Ecco il mio tablener:
MyTAblistener
.
public class MyTabListener implements TabListener {
public SherlockFragment fragment;
MyTabListener(SherlockFragment fr) {
Log.d("MYTAG", "Creating a fragmentListener w/ " + fr);
this.fragment = fr;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("TAG", "" + fragment);
ft.replace(R.id.fragment_container, fragment);
}
}
E la mia attività principale:
Sherlocktestattività
.
public class SherlockTestActivity extends SherlockActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//We take the support actionbar
ActionBar ab = getSupportActionBar();
//We set to navigationmode with tabs
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//we create the tabs
ActionBar.Tab homeTab = ab.newTab().setText("Home");
ActionBar.Tab tagsTab = ab.newTab().setText("Tags");
//We create the fragments
SherlockFragment homeFragment = new HomeFragment();
SherlockFragment tagsFragment = new TagFragments();
//And we set the tabsListener;
homeTab.setTabListener(new MyTabListener(homeFragment));
tagsTab.setTabListener(new MyTabListener(tagsFragment));
Log.d("","" + homeTab);
ab.addTab(homeTab);
ab.addTab(tagsTab);
}
OK, quindi l'errore sembra essere lanciato quando aggiungo la scheda alla mia barra action. E quando non aggiungo il tabulato alla scheda, non ci sono errori.
Il codice ft.replace(R.id.fragment_container, fragment);
(mytablistener) sembra essere il problema, ma non riesco a capire perché. Il frammento non è null (inizializzato quando Instancianding un nuovo tablener), e non c'è motivo per cui il frammento_container è sbagliato.
Quindi se qualcuno può riuscire a aiutarmi qui in giro! Grazie!
Soluzione
Dovresti estendere SherlockFragmentAttività anziché Sherlockattività.Utilizzare un'attività di frammento quando si gestisce i frammenti.