简体   繁体   English

ListView上的Android layoutinflater不起作用

[英]Android layoutinflater on listview not working

I am creating one class with listview which is populated with http json array, i did this before and code is ok. 我正在创建一个用httpview数组填充的listview的类,我之前做过,代码还可以。 But when i install application i got white screeen (didn't force close) in LogCat i dont have any error message. 但是,当我安装应用程序时,我在LogCat中得到了白色的屏幕(没有强制关闭),我没有任何错误消息。 LayoutInflater can't set activity_main. LayoutInflater无法设置activity_main。

this is the activity_main where i declared a listview. 这是我在其中声明列表视图的activity_main。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/black_cool" >

    <ListView
        android:id="@+id/custom_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:dividerHeight="1dp" />

</FrameLayout>

ListView objects is in the list_row_layout.xml no need to post it, everything works. ListView对象位于list_row_layout.xml无需发布它,一切正常。

public class MainActivity extends Activity {
    public ArrayList<FeedItem> feedList;
    public ListView feedListView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        View rootView = inflater.inflate(R.layout.activity_main, container, false);

        String url = "...";
        new DownloadFilesTask().execute(url);
        return rootView;
    }
    public void updateList() {
        feedListView= (ListView) findViewById(R.id.custom_list);
        feedListView.setAdapter(new CustomListAdapter(this, feedList));
    }
}

.... json class and other... .... json类和其他...

What should be a problem? 应该是什么问题? I tried extending a Fragment but same result. 我尝试扩展一个Fragment但结果相同。 I tried adding a onCreate and setContentview but didn't helped me. 我尝试添加onCreatesetContentview但没有帮助我。

Extend Activity override onCreate . 扩展活动覆盖onCreate Initialize listview in onCreate onCreate初始化listview

public ListView feedListView;
@Override 
public void onCreate(bundle savedInstanceState)
{
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main); 
      feedListView= (ListView) findViewById(R.id.custom_list);
      new DownloadFilesTask().execute(url);
}

Try this for extend Activity 试试这个extend Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
           feedListView= (ListView) findViewById(R.id.custom_list);
           String url = "...";
           new DownloadFilesTask().execute(url);
           feedListView.setAdapter(new CustomListAdapter(this, feedList));
}

and Try this for extend Fragment 并尝试使用此extend Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_main, container, false);
            feedListView= (ListView) rootView.findViewById(R.id.custom_list);

            String url = "...";
            new DownloadFilesTask().execute(url);
            feedListView.setAdapter(new CustomListAdapter(this, feedList));
            return rootView;
     }

change this line 改变这条线

feedListView= (ListView) findViewById(R.id.custom_list);

into. 入。

feedListView= (ListView) getView().findViewById(R.id.custom_list);

this is for fragment. 这是片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM