Android layoutinflater on listview not working

718 views Asked by At

I am creating one class with listview which is populated with http json array, i did this before and code is ok. But when i install application i got white screeen (didn't force close) in LogCat i dont have any error message. LayoutInflater can't set activity_main.

this is the activity_main where i declared a listview.

<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.

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...

What should be a problem? I tried extending a Fragment but same result. I tried adding a onCreate and setContentview but didn't helped me.

3

There are 3 answers

0
Hariharan On BEST ANSWER

Try this for 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

@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;
     }
0
Raghunandan On

Extend Activity override onCreate. Initialize listview in 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);
}
0
kalyan pvs On

change this line

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

into.

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

this is for fragment.