我有一个类PostedJobHistory(片段):
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;
}
}
对于适配器,我将此类放在与上面相同的.java文件中:
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:致命异常:主要 处理:com.example.ankit.job_depot,PID:17392 java.lang.ClassCastException:android.app.Application无法强制转换为android.app.Activity 在com.example.ankit.job_depot.TimelineList.getView(PostedJobHistory.java:80) 在android.widget.AbsListView.obtainView(AbsListView.java:2347) 在android.widget.ListView.measureHeightOfChildren(ListView.java:1270) 在android.widget.ListView.onMeasure(ListView.java:1182) 在android.view.View.measure(View.java:17547) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463) 在android.view.View.measure(View.java:17547) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1455) 在android.view.View.measure(View.java:17547) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
here给出Context不能被转换为Activity,因为它不是它的子类。我不知道如何解决这个问题。
答案 0 :(得分:1)
您必须尝试将getApplicationContext()
而不是Activity
传递给fragment
的构造函数。尝试传递活动实例而不是应用程序上下文。
要PostedJobHistory
constructor
,请使用this
答案 1 :(得分:0)
这是错误的
public PostedJobHistory(Context context, String employerName) {
this.context = context;
this.employerName = employerName;
}
片段应该没有参数构造函数
其次,您可以使用getActivity()
获取活动的上下文
onCreateView
listViewJobHistory.setAdapter(new TimelineList(getActivity(), jobDetails));
然后在适配器的构造函数中
LayoutInflate inflater;
public TimelineList(Context mContext, List<ParseObject> elements) {
inflater = LayoutInflater.from(mContext);
this.elements = elements;
}
如果您需要将数据从活动传递到片段
Send data from activity to fragment in android
此外,您应该使用ViewHolder
模式
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
注意: Fragment的所有子类必须包含公共无参数构造函数。
来源:
http://developer.android.com/reference/android/app/Fragment.html
答案 2 :(得分:-2)
您正在将Context转换为Activity,这就是您收到错误的原因。
请在构造函数的参数中传递Activity而不是Context
Activity mActivity ;
public TimelineList(Activity mActivity, List<ParseObject> elements) {
this.mActivity= mActivity;
this.elements = elements;
}