0

Good day,

I am having the problem said in the title when app runs on a tablet. My intention as shown in the layout is to show to the right, an empty framelayout the first time user enters the app so nothing is displayed. So far so good.

Then user clicks on an item of the listview on the left pane, so its detail is displayed on the right pane. This is well captured in the listener in the Activity below that triggers the else part, and tries to add the FragmentDetail programatically, that has its corresponding layout.

@Override
public void onItemSelected(Uri wifi)
{
    mWifi = wifi;
    if (wifi != null)
    {
        if (!mDualFragments)
        {
            Intent intent = new Intent(this, DetailActivity.class);
            intent.setData(wifi);
            startActivity(intent);
        }
        else
        {
            // add fragment programmatically to the framelayout
            FragmentDetail fd = new FragmentDetail();
            getFragmentManager()
                    .beginTransaction()
                    .add(R.id.mFragment_detail, fd)
                    .commit();
            fd.updateFragmentDetail(wifi, true);
        }
    }
}

Then, the following method in FragmentDetail is called, and the error occurs in the setTitle getActivity(). Looks that the error is in the FragmentTransaction in the caller activity.

    public void updateFragmentDetail(Uri wifi, boolean dualFragments)
    {
    mCurrentWifiUri = wifi;
    mDualFragments = dualFragments;
    if (mCurrentWifiUri != null)
    {
        getActivity().setTitle(R.string.title_editor_activity);
        if (!dualFragments)
        {
            getLoaderManager().initLoader(EXISTING_WIFI_LOADER, null, this);
        }
        else
        {
            getLoaderManager().restartLoader(EXISTING_WIFI_LOADER, null, this);
        }
    }
}

This is the Layout for tablet

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="eu.javimar.wirelessvlc.MainActivity"
tools:showIn="@layout/activity_main">

<fragment
    android:id="@+id/mFragment_list"
    android:layout_weight="30"
    android:layout_width="0px"
    android:layout_height="match_parent"
    class="eu.javimar.wirelessvlc.view.FragmentList" />

<FrameLayout
    android:id="@+id/mFragment_detail"
    android:layout_weight="70"
    android:layout_width="0px"
    android:layout_height="match_parent">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:layout_margin="16dp"
        android:textSize="25sp"
        android:text="Empty View, Click on a Wi-Fi" />
</FrameLayout>

Thanks!

UPDATE I am confused. How is this related to a NPE Q&A as the one you mention @gabe-sechan? I know this is a NPE issue and that getActivity() is creating it. But this is solely an issue related to FragmentTransactions, asynchronicity, and that it won't be attached to the Activity, as well answered by @mike-m. Perhaps I'm missing something?

JaviMar
  • 375
  • 5
  • 18
  • 2
    `FragmentTransaction`s are asynchronous. Your `Fragment` won't be attached to the `Activity` when you call `updateFragmentDetail()` on it right there. – Mike M. Dec 19 '16 at 00:13
  • You notify people in comments. @ within a question does nothing – OneCricketeer Dec 19 '16 at 09:06
  • Anyway, if you get a NullPointerException, it's not really that we flag "what" sometimes but "why" and "how to fix" requires you to look at the stacktrace, see what's causing it, then go read the documentation. getActivity might just mention exactly when it returns null – OneCricketeer Dec 19 '16 at 09:09

0 Answers0