listview上的Android layoutinflater无法正常工作

时间:2013-11-29 12:13:16

标签: java android layout inflate

我正在用listview创建一个类,其中填充了http json数组,之前我做过这个并且代码没问题。但是当我安装应用程序时,我在LogCat中得到了白色屏幕(没有强制关闭)我没有任何错误消息。 LayoutInflater无法设置activity_main。

这是我宣布列表视图的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对象位于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等等......

应该是什么问题?我尝试扩展Fragment但结果相同。我尝试添加onCreate和setContentview但没有帮助我。

3 个答案:

答案 0 :(得分:1)

扩展活动覆盖onCreate。在onCreate

中初始化列表视图
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);
}

答案 1 :(得分:1)

试试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));
}

并尝试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;
     }

答案 2 :(得分:1)

更改此行

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

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

这是片段。