-1

I have a class postedJobHistory (Fragment):

public class PostedJobHistory extends Fragment {
    String employerName;
    private List<ParseObject> jobDetails;
    Context context;
    public PostedJobHistory() {}
    public PostedJobHistory(Context context, String employerName) {
        this.context = context;
        this.employerName = employerName;
    }
    ListView listViewJobHistory;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View jobsView = inflater.inflate(R.layout.fragment_posted_job_history, container, false);
        listViewJobHistory = (ListView) jobsView.findViewById(R.id.listViewJobHistory);

        if(jobDetails==null){
            jobDetails=new ArrayList<ParseObject>();
            EmployerHistory jobsQuery=new EmployerHistory();
            jobDetails = jobsQuery.getJobHistory(employerName);
        }

        listViewJobHistory.setAdapter(new TimelineList(context, jobDetails));

        return jobsView;
    }
}

For the Adapter I have this class in the same .java file as above:

class TimelineList extends BaseAdapter {
    private List<ParseObject> elements;
    Context mContext;
    public TimelineList(Context mContext, List<ParseObject> elements) {
        this.mContext = mContext;
        this.elements = elements;
    }


    @Override
    public int getCount() {
        return elements.size();
    }

    @Override
    public ParseObject getItem(int position) {
        return elements.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//this line is giving runtime exception
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            View row;
            row = inflater.inflate(R.layout.job_history_listitem, parent, false);
            TextView title, detail;
            title = (TextView) row.findViewById(R.id.title);
            detail = (TextView) row.findViewById(R.id.detail);
            title.setText(getItem(position).get("JobName").toString());
            title.setMovementMethod(LinkMovementMethod.getInstance());
            detail.setText(getItem(position).get("updatedAt").toString());
            return (row);
        }
    }

07-28 02:06:12.424 17392-17392/com.example.ankit.job_depot E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.ankit.job_depot, PID: 17392 java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity at com.example.ankit.job_depot.TimelineList.getView(PostedJobHistory.java:80) at android.widget.AbsListView.obtainView(AbsListView.java:2347) at android.widget.ListView.measureHeightOfChildren(ListView.java:1270) at android.widget.ListView.onMeasure(ListView.java:1182) at android.view.View.measure(View.java:17547) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463) at android.view.View.measure(View.java:17547) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1455) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)

here it is given that Context cannot be casted to an Activity as it is not its subclass. I am not sure how to resolve this issue.

Community
  • 1
  • 1
Bharthan
  • 1,458
  • 2
  • 17
  • 29

3 Answers3

1

This is wrong

public PostedJobHistory(Context context, String employerName) {
    this.context = context;
    this.employerName = employerName;
}

Fragment should have no argument constructor

Secondly you can get context of activity using getActivity() in onCreateView

listViewJobHistory.setAdapter(new TimelineList(getActivity(), jobDetails));

Then in Constructor of your adapter

LayoutInflate inflater;
public TimelineList(Context mContext, List<ParseObject> elements) {
    inflater = LayoutInflater.from(mContext);
    this.elements = elements;
}

If you need to pass data from activity to fragment

Send data from activity to fragment in android

Also you should be using a ViewHolder pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Note: All subclasses of Fragment must include a public no-argument constructor.

Source :

http://developer.android.com/reference/android/app/Fragment.html

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks, this is what I was looking for. Thanks for pointing out how to pass data from activity to fragment, android studio was giving warning but I was giving red flags, but I was not sure how to do it. Thanks a lot!!! – Bharthan Jul 28 '15 at 06:20
  • @Bharthan i hope you have no arg constructor. check the edited answer – Raghunandan Jul 28 '15 at 06:40
1

You must be trying to pass getApplicationContext() instead of Activity to the constructor of your fragment.Try passing the activity instance instead of application context.

To PostedJobHistory constructor, pass the parent activity instance using this

Renjith
  • 3,274
  • 19
  • 39
-2

You are casting Context into Activity that's why you getting error.

Please pass Activity in parameter of constructor instead of Context

Activity mActivity ;
    public TimelineList(Activity mActivity, List<ParseObject> elements) {
        this.mActivity= mActivity;
        this.elements = elements;
    }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • `java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity` – Raghunandan Jul 28 '15 at 06:25