Search code examples
androidandroid-fragmentsandroid-listfragment

Issue with Fragment on Android Developer Page


I was reading the documentation on Fragment on the Developer site. I created a new project targeting api 8:android 2.2. I have the Android Support Library install, and I added the android support library to the project. I started implementing the helper method showDetails, and I get the red squiggly line saying that "a_item could not be resolved". I don't remember the instruction saying create an a_item variable or add a layout with a_item id. In addition, in the detail activity, I am getting an error message too. (getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();) It says "The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, DetailsFragment)". Did I miss something in the instructions?

Here are my sample codes:

package com.example.fragmentlayout;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getResources().getConfiguration().ORIENTATION_LANDSCAPE == Configuration.ORIENTATION_LANDSCAPE )
        {
            finish();
            return;
        } 
        if(savedInstanceState == null)
        {
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }

}

Here is the Titles Fragment:

package com.example.fragmentlayout;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TitlesFragment extends ListFragment {
   boolean mDualPane;
   int mCurCheckPosition =0;
    String[] titles = {"Title 1", "Title 2", "Title 3"};
    String[] Details = {"Details for one", "Details for two", "Details for Three"};

    @SuppressLint("InlinedApi")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, titles));

        View detailsFrame = getActivity().findViewById(R.id.fragment2);
        mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

        if(savedInstanceState != null)
        {
            mCurCheckPosition = savedInstanceState.getInt("curChoise", 0);
        }

        if(mDualPane)
        {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showDetails(mCurCheckPosition);
        }
    }

   private void showDetails(int index) {

        mCurCheckPosition = index;
        if(mDualPane)
        {
            getListView().setItemChecked(index, true);
            DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.fragment2);
            if(details == null || details.getShownIndex() != index)
            {
                details = DetailsFragment.newInstance(index);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if(index ==0)
                {
                    ft.replace(R.id.fragment2, details);
                } else
                {
                    ft.replace(R.id.a_item, details);
                }
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        } else
        {
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }


}

Here is the detailsFragment:

package com.example.fragmentlayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

public class DetailsFragment extends Fragment {
    String[] Details = {"Details for one", "Details for two", "Details for Three"};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if(container == null)
        {
            return null;
        }
        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        text.setText(Details[getShownIndex()]);
        return scroller;

    //  return super.onCreateView(inflater, container, savedInstanceState);
    }

    public static DetailsFragment newInstance(int index)
    {
        DetailsFragment f = new DetailsFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex()
    {
        return getArguments().getInt("index", 0);
    }
}

Here is the land and portrait layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentlayout.TitlesFragment"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentlayout.DetailsFragment"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentlayout.TitlesFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Solution

  • You need to use getSupportFragmentManager instead of getFragmentManager, because you are using support fragments.

    The R.id.a_item references the id of the layout in your layout file. Instead of android:id="@+id/fragment1", make it android:id="@+id/a_item"