0

Hope it's my last issue with Fragments :)

I have two fragments (one regular the other list), and when I run the setContentView on main_activity I notice that the onCreate of the regular fragment is being called, on the other hand the ListFragment onCreate is not being called.

Both of them are pretty much the same except that I didn't override onAttach + onDetach in the ListFragment (it was done for callbacks in the regular one). What I did overwrote is: onCreate, onCreateView, onActivityCreated, onStart (to start an asynctask), onSaveInstanceState.

Can you tell me what the prblem is? since I get NullPointerEception in the fragmentManager for the ListFragment.

Somecode:

activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mynews.mynews.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.mynews.mynews.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

    <fragment android:id="@+id/rss_feed"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:name="com.kfir.bs.mynews.mynews.NewsFeed"
        tools:layout="@layout/fragment_news_feed" />

</android.support.v4.widget.DrawerLayout>

On Activity:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            setContentView(R.layout.activity_main);
            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getFragmentManager().findFragmentById(R.id.navigation_drawer);
            mNewsFeed = (NewsFeed) getFragmentManager().findFragmentById(R.id.rss_feed);

On Fragment:

public class NavigationDrawerFragment extends Fragment {
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
    private NavigationDrawerCallbacks mCallbacks;
    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ListAdapter adapter;
    private ListView mDrawerListView;
    private View mFragmentContainerView;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;
    private boolean mUserLearnedDrawer;

    public NavigationDrawerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }

        adapter = new ListAdapter(getActionBar().getThemedContext(), generateData());

        selectItem(mCurrentSelectedPosition);
    }

On ListFragment:

public class NewsFeed extends ListFragment
{
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

    View feedView;
    ListAdapterFeed rssListAdapter;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;

    public NewsFeed() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

Adding the stacktrace:

https://www.dropbox.com/s/dxrz0925u3nfmgp/ddms7983186720865326533.trace

LogCat:

04-19 23:15:49.901 11473-11473/com.kfir.bs.mynews.mynews E/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file 04-19 23:15:49.901 11473-11473/com.kfir.bs.mynews.mynews E/SELinux﹕ Function: selinux_android_load_priority [1], There is no sepolicy version file 04-19 23:15:49.901 11473-11473/com.kfir.bs.mynews.mynews E/SELinux﹕ Function: selinux_android_load_priority , loading version is VE=SEPF_GT-N7100_4.3_0018 04-19 23:15:49.901 11473-11473/com.kfir.bs.mynews.mynews E/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts 04-19 23:15:49.906 11473-11473/com.kfir.bs.mynews.mynews D/dalvikvm﹕ Late-enabling CheckJNI 04-19 23:15:49.991 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ Sending WAIT chunk 04-19 23:15:49.991 11473-11473/com.kfir.bs.mynews.mynews W/ActivityThread﹕ Application com.kfir.bs.mynews.mynews is waiting for the debugger on port 8100... 04-19 23:15:50.036 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕ Debugger is active 04-19 23:15:50.191 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ Debugger has connected 04-19 23:15:50.191 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:50.391 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:50.591 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:50.791 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:50.996 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:51.196 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:51.396 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:51.596 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:51.796 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ waiting for debugger to settle... 04-19 23:15:51.996 11473-11473/com.kfir.bs.mynews.mynews I/System.out﹕ debugger has settled (1310) 04-19 23:15:52.911 11473-11473/com.kfir.bs.mynews.mynews D/dalvikvm﹕ threadid=1: still suspended after undo (sc=1 dc=1) 04-19 23:16:17.531 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕ TRACE STARTED: '[DDMS]' 8192KB 04-19 23:16:17.546 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm﹕ +++ active profiler count now 1 04-19 23:16:24.391 11473-11473/com.kfir.bs.mynews.mynews E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView 04-19 23:16:27.771 11473-12572/com.kfir.bs.mynews.mynews I/dalvikvm﹕ threadid=11: waiting for method trace to finish 04-19 23:16:48.961 11473-11473/com.kfir.bs.mynews.mynews E/MyNews﹕ Exceptionjava.lang.NullPointerException 04-19 23:17:15.451 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm﹕ +++ active profiler count now 0 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕ TRACE STOPPED: writing 44458 records 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews W/dalvikvm﹕ WARNING: a debugger is active; method-tracing results will be skewed

Thanks in advance, K

AgentK
  • 75
  • 7
  • Just missing the logcat. – Blo Apr 19 '14 at 19:57
  • I can't seem to get logcat in Android Studio when catching exceptions... – AgentK Apr 19 '14 at 19:58
  • *"I get NullPointerEception in the fragmentManager for the ListFragment"* - how do you know that without logcat or stacktrace? – Blo Apr 19 '14 at 20:03
  • I use the debugger to check the flow, do you want a stack trace? – AgentK Apr 19 '14 at 20:04
  • Yes, sorry to use logcat name.. – Blo Apr 19 '14 at 20:09
  • Added trace and logcat – AgentK Apr 19 '14 at 20:21
  • It will be better to provide your stacktrace here in code format (because I can't open it from my machine, sorry). However, put here the revelant part for your issue. Also, I might help someone to find the issue faster. Thank you. – Blo Apr 19 '14 at 20:55
  • Openning a trace file is pretty much straghit forward you need the DDMS from the sdk, see here http://stackoverflow.com/questions/4677144/how-can-i-open-trace-file-formattraceview-without-ddms – AgentK Apr 20 '14 at 04:47
  • Any ideas? It seems weird, it struck me that maybe it's because of the drawer layout in activity_main? – AgentK Apr 20 '14 at 08:20

0 Answers0