0

I have public class PageFragment that extends Fragment to create 6 pages with views that fills by another class. Here are the PageFragment class:

public class PageFragment extends Fragment {
static final String arg_page_number = "arg_page_number";

int pageNumber;
int backColor;
public LinearLayout framesContainer;
ExecutorService ex = Executors.newCachedThreadPool();
Future<ArrayList<ElementData>> s = ex.submit(new MyThread());

public static PageFragment newInstance(int page) {
    PageFragment pageFragment = new PageFragment();
    Bundle arguments = new Bundle();
    arguments.putInt(arg_page_number, page);
    pageFragment.setArguments(arguments);
    return pageFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pageNumber = getArguments().getInt(arg_page_number);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.table_row, null);
    framesContainer = (LinearLayout) view.findViewById(R.id.frames_container);
    for (int i = 0; i < 20; i += 4) {
        Frame frame = new Frame(getApplicationContext());
        try {
            frame.setStudyName(s.get().get(0).Days().get(i));
            frame.setStudyKindName(s.get().get(0).Days().get(i + 1));
            frame.setAuditorium(s.get().get(0).Days().get(i + 2));
            frame.setLectureTitle(s.get().get(0).Days().get(i + 3));
            framesContainer.addView(frame);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    return view;
}

class MyThread implements Callable<ArrayList<ElementData>> {
    public ArrayList<ElementData> call() {
        ArrayList<ElementData> elementDataArrayList = Parser.parse("url");
        return elementDataArrayList;
    }
}}

And this is MyActivity:

public class MyActivity extends FragmentActivity {
/**
 * Called when the activity is first created.
 */
static final String TAG = "myLogs";
static final int PAGE_COUNT = 6;
ViewPager pager;
PagerAdapter pagerAdapter;
SharedPreferences sPref;
String[] groups = {....};
final String SAVED_TEXT = "SavePref";
private LinearLayout framesContainer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sPref = getPreferences(MODE_PRIVATE);
    if (sPref.getBoolean(SAVED_TEXT, true)) {
        setContentView(R.layout.startpage);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, groups);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setAdapter(adapter);

        final int selectionCurrent = spinner.getSelectedItemPosition();
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                if (selectionCurrent != position) {
                    sPref = getPreferences(MODE_PRIVATE);
                    SharedPreferences.Editor ed = sPref.edit();
                    ed.putBoolean(SAVED_TEXT, false);
                    ed.commit();
                    setContentView(R.layout.main);


                    pager = (ViewPager) findViewById(R.id.frames_container);
                    pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
                    pager.setAdapter(pagerAdapter);

                    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                            Log.d(TAG, "onPageSelected, position = " + position);

                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                    });
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }
        });
    } else {
        setContentView(R.layout.main);
        pager = (ViewPager) findViewById(R.id.frames_container);
        pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);

        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected, position = " + position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PageFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }
}}

When i'm using getApplicationContext is shows me error of unresolved method. Do i need to cast MyActivity's context here? How can i do that?

Frame class:

public class Frame extends RelativeLayout {
    private TextView StudyName;
    private TextView Auditorium;
    private TextView StudyKindName;
    private TextView LectureTitle;

    public Frame(Context context) {
        super(context);
        initComponent();
    }

    private void initComponent() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.table_row, this);
        StudyName = (TextView) findViewById(R.id.StudyName);
        Auditorium = (TextView) findViewById(R.id.Auditorium);
        StudyKindName = (TextView) findViewById(R.id.StudyKindName);
        LectureTitle = (TextView) findViewById(R.id.LectureTitle);
    }

    public void setStudyName(String study_Name) {
        StudyName.setText(study_Name);
    }

    public void setAuditorium(String auditorium) {
        Auditorium.setText(auditorium);
    }

    public void setStudyKindName(String studyKindName) {
        StudyKindName.setText(studyKindName);
    }

    public void setLectureTitle(String lectureTitle) {
        LectureTitle.setText(lectureTitle);
    }

}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/frames_container"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">
    </android.support.v4.view.ViewPager>

</RelativeLayout>

table_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/program_frame"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip">

    <TextView
        android:id="@+id/StudyName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white"
        android:textStyle="normal"
        android:textSize="16sp"
        android:text="StudyName"/>
    <TextView
        android:id="@+id/Auditorium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold"
        android:textSize="16sp"
        android:singleLine="true"
        android:text="Auditorium"/>
    <TextView
        android:id="@+id/StudyKindName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/StudyName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold"
        android:textSize="15sp"
        android:singleLine="true"
        android:text="StudyKindName"/>
    <TextView
        android:id="@+id/LectureTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/StudyKindName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:textColor="@android:color/darker_gray"
        android:textStyle="normal"
        android:textSize="12sp"
        android:text="Lector"/>
    <ImageView
        android:id="@+id/imageView2"
        android:layout_below="@id/LectureTitle"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="15dip"
        android:background="#bb333333" />
</RelativeLayout>
LingWillOk
  • 25
  • 6
  • you can cut down unrelated code. and indicate the line where the problem occurs – Raghunandan May 18 '14 at 13:00
  • you are supposed to post a new question for a new problem. this has completely changed the question to the one i answered which is not right. the view pager is in activity layout – Raghunandan May 18 '14 at 14:22

1 Answers1

0

You can use getActivity().getApplicationContext() in Fragment.

I don't know what Frame is in Frame frame = new Frame(getApplicationContext());. In case you are doing ui stuff use only getActivity().

Read

When to call activity context OR application context?

Community
  • 1
  • 1
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
  • ugh seems like i hastened with closing the topic. I've added Frame class to the topic. Now i'm getting NullPointerException in framesContainer.addView(frame), looks like it doesn't set data to TextViews. – LingWillOk May 18 '14 at 13:44
  • `framesContainer` is null. And a new question post a new thread. can't solve everything under one thread – Raghunandan May 18 '14 at 13:48
  • Thank you, sorry for that. Will try to figure out why it's null. – LingWillOk May 18 '14 at 13:52
  • check the id `frames_container` @ `framesContainer = (LinearLayout) view.findViewById(R.id.frames_container);`. is it the same in the xml layout also – Raghunandan May 18 '14 at 13:53
  • and for views use `getActivity()` – Raghunandan May 18 '14 at 13:54
  • Should I start a new topic for this? Problem of 'NullPointerException' has gone by changing 'view.findViewByID' to 'getActivity().findViewByID' but 'framesContainer.addView(frame)' still doesn't fills up by data and moreover there is only one view at the screen but there should be 5 at one screen. – LingWillOk May 18 '14 at 14:12
  • that means the view doe snot belong to the fragment layout. Yes youa re supposed to post the xml where you have the view and the fragment code. rest all unnecessary – Raghunandan May 18 '14 at 14:13
  • where do you have view with id `frames_container` in the posted xml. that's your clue and start new thread – Raghunandan May 18 '14 at 14:18
  • Sorry for awaiting, started new thread: http://stackoverflow.com/questions/23724799/swiping-the-pages-with-viewpage – LingWillOk May 18 '14 at 17:26