Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:11:48+00:00 2026-06-13T23:11:48+00:00

I got an error after i tried to implement listview into my fragments. Please

  • 0

I got an error after i tried to implement listview into my fragments. Please check on my codes, I’m trying to inflate an xml layout for my listview besides using ListView lv = getListView(); . I know that method works but I do not want that. Please help me correct my codes.

LOGCAT:

11-07 10:54:36.828: E/AndroidRuntime(2947): FATAL EXCEPTION: main
11-07 10:54:36.828: E/AndroidRuntime(2947): java.lang.NullPointerException
11-07 10:54:36.828: E/AndroidRuntime(2947):     at testing.app.FreeFragment$loadListView.onPostExecute(FreeFragment.java:132)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at testing.app.FreeFragment$loadListView.onPostExecute(FreeFragment.java:1)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at android.os.AsyncTask.finish(AsyncTask.java:417)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at android.os.Looper.loop(Looper.java:130)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at android.app.ActivityThread.main(ActivityThread.java:3691)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at java.lang.reflect.Method.invoke(Method.java:507)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-07 10:54:36.828: E/AndroidRuntime(2947):     at dalvik.system.NativeStart.main(Native Method)

mainactivity.java

public class MainActivity extends SherlockFragmentActivity {
    Tab tab;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        AppRater.app_launched(this);
        if (!isNetworkAvailable()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Internet Connection Required")
                    .setCancelable(false)
                    .setPositiveButton("Exit",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    finish();
                                }

                            });
            AlertDialog alert = builder.create();
            alert.show();

        } else {

            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);




            tab = actionBar.newTab().setTabListener(new FreeFragment())
                    .setIcon(R.drawable.free);
            actionBar.addTab(tab);

            tab = actionBar.newTab().setTabListener(new BuyFragment())
                    .setIcon(R.drawable.buy);
            actionBar.addTab(tab);

            tab = actionBar.newTab().setTabListener(new ReaderFragment())
                    .setIcon(R.drawable.reader);
            actionBar.addTab(tab);
        }
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }



}

freefragment.java

 public class FreeFragment extends Fragment implements
        ActionBar.TabListener {

    static final String URL = "https://myxml.xml";
    static final String KEY_SONG = "song";
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_CAT_ARTIST = "artistcat";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    static final String KEY_BIG_URL = "big_url";
    static final String KEY_CAT_URL = "cat_url";
    static final String KEY_DESC = "cat_desc";
    ArrayList<HashMap<String, String>> menuItems;
    ListAdapter adapter;
    private Fragment mFragment;

    ListView lv;

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

            View v = inflater.inflate(R.id.listview); //<--- what do i inflate here? 
            lv = (ListView) v.findViewById(R.id.list);

            if (!isNetworkAvailable()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("Internet Connection Required")
                        .setCancelable(false)
                        .setPositiveButton("Exit",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        FileCache loader = new FileCache(null);
                                        loader.clear();  
                                        getActivity().finish();
                                    }

                                });
                AlertDialog alert = builder.create();
                alert.show();

            } else {

                new loadListView().execute();

            }


            return v;
    }



    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

    public class loadListView extends AsyncTask<Integer, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Integer... args) {
            // updating UI from Background Thread
            menuItems = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
                map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL));
                map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
                // adding HashList to ArrayList
                menuItems.add(map);
            }
            return null;
        }

        @Override
        protected void onPostExecute(String args) {

            if (getActivity() != null) {
                adapter = new MainPageLazyAdapter(getActivity(), menuItems);
                lv = (ListView)getActivity().findViewById(R.id.list);                                 
                //setListAdapter(adapter);
                lv.setAdapter(adapter);


            }

        }
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mFragment = new FreeFragment();
        ft.add(android.R.id.content, mFragment);
        ft.attach(mFragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(mFragment);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}

ListView.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

     <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_centerHorizontal="true"
                         ads:adUnitId="mycode"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="true"
                         />


<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/adView"
    android:cacheColorHint="#00000000" >

</ListView>
</RelativeLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:11:49+00:00Added an answer on June 13, 2026 at 11:11 pm

    Of course the ListView is null.

    This:

                @Override
                protected void onPostExecute(String args) {
    
                    if (getActivity() != null) {
                        adapter = new MainPageLazyAdapter(getActivity(), menuItems);
                        lv.setAdapter(adapter);
    
                        lv = (ListView)getActivity().findViewById(R.id.list);
    
    
                    }
    
                }
    

    needs to be this:

                @Override
                protected void onPostExecute(String args) {
    
                    if (getActivity() != null) {
                        adapter = new MainPageLazyAdapter(getActivity(), menuItems);
                        lv = (ListView)getActivity().findViewById(R.id.list);                                 
    
                        lv.setAdapter(adapter);
    
    
                    }
    
                }
    

    you can’t set the adapter on the ListView before you initialize the ListView.

    More importantly, you’re doing this all wrong: you’re using a SherlockListFragment which itself extends ListFragment which you can think of as being similar to a ListActivity – it just gives you some conveniences for setting up a listview. You need to just call setListAdapter in your postExecute. You don’t need to inflate any listview yourself; thats the whole point of a ListFragment.

    EDIT:

    Since, as you indicated, you want to have more than just a ListView inside of your Fragment, don’t use SherlockListFragment. Instead, extend a plain old Fragment. Then inside of onCreateView, do this:

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.listview);
            lv = v.findViewById(R.id.list);
            return v;
    }
    

    Now you have a reference to that ListView as a field. Now, in your onPostExecute, you can do lv.setAdapter(adapter);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got insufficient memory error after sometime when i built it. I have tried
I'm trying to insert a String into a list. I got this error: TypeError:
I'm trying out the Flaskr tutorial(http://flask.pocoo.org/docs/) and I got the following error after I
I tried to install a plugin for eclipse,but get an error,after some googling,got a
I got build error after run in Snow Leopard (MacPort v.1.8.0) sudo port install
I have odd problem: After starting server I got this error: undefined local variable
After pushing to a git repo, I got this error: [trusktr@starlancer bml]$ git push
After I upgrade to Node 0.6.18, I got the error: listen EINVAL when start.
After the sub of the exec JCL, i've got the following error : SQLCODE
I got this problem after deploying my web package to IIS: HTTP Error 500.19

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.