Android LayoutInflater问题

时间:2012-06-05 22:18:06

标签: android layout-inflater

这是我的类DownloadableProjectsFromWebAdapter的getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    DownloadableProjectsFromWebHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new DownloadableProjectsFromWebHolder();
        holder.txtPid = (TextView)row.findViewById(R.id.tvAllProjectsPid);
        holder.txtName = (TextView)row.findViewById(R.id.tvAllProjectsName);
        holder.txtShareable = (TextView)row.findViewById(R.id.tvAllProjectsShareable);

        row.setTag(holder);
    }
    else
    {
        holder = (DownloadableProjectsFromWebHolder)row.getTag();
    }

    String share_text;
    AllProjectListInfo li = data.get(position);
    holder.txtPid.setText("ID: " + Integer.toString(li.getId()));
    holder.txtName.setText(li.getName());
    if (li.getShareable()){
        share_text = "Public Project";
    } else {
        share_text = "Private Project";
    }
    holder.txtShareable.setText(share_text);

    return row;
}

导致问题的行:

35:    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
36:    row = inflater.inflate(layoutResourceId, parent, false);

这里来自LogCat的信息:

FATAL EXCEPTION: main
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
    at com.ericbrian.tnc.ConProForAndroid.DownloadableProjectsFromWebAdapter.getView(DownloadableProjectsFromWebAdapter.java:36)
    at android.widget.AbsListView.obtainView(AbsListView.java:2033)
    at android.widget.ListView.makeAndAddView(ListView.java:1772)
    at android.widget.ListView.fillDown(ListView.java:672)
    at android.widget.ListView.fillFromTop(ListView.java:732)
    at android.widget.ListView.layoutChildren(ListView.java:1625)
    at android.widget.AbsListView.onLayout(AbsListView.java:1863)
    at android.view.View.layout(View.java:11278)
    at android.view.ViewGroup.layout(ViewGroup.java:4224)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    at android.view.View.layout(View.java:11278)
    at android.view.ViewGroup.layout(ViewGroup.java:4224)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    at android.view.View.layout(View.java:11278)
    at android.view.ViewGroup.layout(ViewGroup.java:4224)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    at android.view.View.layout(View.java:11278)
    at android.view.ViewGroup.layout(ViewGroup.java:4224)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    at android.view.View.layout(View.java:11278)
    at android.view.ViewGroup.layout(ViewGroup.java:4224)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

这里调用适配器:

DownloadableProjectsFromWebAdapter adapter = 
    new DownloadableProjectsFromWebAdapter(
        getBaseContext(), 
        R.layout.all_projects_listview_item, 
        results);

最后,结果定义:

List<AllProjectListInfo> results

现在,我从另一个使用自定义适配器的地方复制了此代码。但是,那个工作得很好。我不明白为什么这个没有。

感谢任何帮助。

感谢。

2 个答案:

答案 0 :(得分:15)

而不是(这大多是错误的)

LayoutInflater inflater = ((Activity)context).getLayoutInflater();

这样做:

LayoutInflater inflater = LayoutInflater.from(context);

答案 1 :(得分:0)

您在适配器中设置错误的上下文:

试试这个:

DownloadableProjectsFromWebAdapter adapter = 
new DownloadableProjectsFromWebAdapter(
    MyActivityClass.this, 
    R.layout.all_projects_listview_item, 
    results);