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

  • SEARCH
  • Home
  • 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 7075207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:08:59+00:00 2026-05-28T06:08:59+00:00

How do I get a ListView using a SimpleCursorAdapter to show in a ViewPager?

  • 0

How do I get a ListView using a SimpleCursorAdapter to show in a ViewPager? The Pager alone works just fine and the ListView works ok in a ListActivity, but I dont know how to make it works in an Activity (maybe it’s the problem).

public class Player extends Activity {
private ViewPager pager;
private static int NUMERO_DE_TELAS = 3;
private AdaptadorDeTelas adaptador;

//Para debug apagar no final    
final String BABAS = "babas";

ContentResolver mContentResolver;

ListView listView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Adaptador para criar o viewpager
    adaptador = new AdaptadorDeTelas();
    pager = (ViewPager)findViewById(R.id.pageradapter);
    pager.setAdapter(adaptador);        

    pager.setCurrentItem(1);       





}
private class AdaptadorDeTelas extends PagerAdapter{

    @Override
    public int getCount(){
        return NUMERO_DE_TELAS;
    }

    @Override
    public Object instantiateItem(View collection, int position){           


        View layout;
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(position == 0){
        layout = inflater.inflate(R.layout.layout_zero, null);
        }else if(position == 1){
            layout = inflater.inflate(R.layout.layout_um, null);
        }else{


            Uri todasMusicas = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

            //Seleciona as tabelas a serem retornadas do content audio.media
            String[] projection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE
            };

            //Pega todas as músicas no aparelho
            String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";


            Cursor cursor;

            cursor = Player.this.managedQuery(
                todasMusicas,
                projection,
                selection,
                null,
                MediaStore.Audio.Media.TITLE + " ASC");



       String[] from = new String []{MediaStore.Audio.Media.TITLE};
       int[] to = new int[] {R.id.textView1};

        View v = (View) findViewById(R.layout.layout_dois);

        listView = (ListView) findViewById(R.id.listView1);
        SimpleCursorAdapter meuAdaptador = new SimpleCursorAdapter(Player.this,         R.layout.layout_dois, cursor, from, to);

        listView.setAdapter(meuAdaptador);

        layout = listView;;
        }



        ((ViewPager)collection).addView((View) layout, 0);                   

        return layout;


    }


    @Override
    public void destroyItem(View collection, int position, Object view){
        ((ViewPager) collection).removeView((View)view);

    }

    @Override
    public boolean isViewFromObject(View view, Object object){
        return view==((View)object);
    }

    @Override
    public void finishUpdate(View arg0){}

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1){}

    @Override
    public Parcelable saveState(){
        return null;
    }

    @Override
    public void startUpdate(View arg0){}


}

I’m trying to show a list of the musics in the sd card in one of the pages of the ViewPager, thanks in advance for anyone who helpme out.

Ps: Sorry for the bad english and for the portuguese in the comments in the code.

This is the error that shows in the LogCat:

01-15 16:48:41.623: W/dalvikvm(270): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 16:48:41.653: E/AndroidRuntime(270): FATAL EXCEPTION: main
01-15 16:48:41.653: E/AndroidRuntime(270): java.lang.NullPointerException
01-15 16:48:41.653: E/AndroidRuntime(270):  at br.com.babas.player.Player$AdaptadorDeTelas.instantiateItem(Player.java:113)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.View.dispatchAttachedToWindow(View.java:6016)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1116)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewRoot.performTraversals(ViewRoot.java:727)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.os.Looper.loop(Looper.java:123)
01-15 16:48:41.653: E/AndroidRuntime(270):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 16:48:41.653: E/AndroidRuntime(270):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:48:41.653: E/AndroidRuntime(270):  at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:48:41.653: E/AndroidRuntime(270):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:48:41.653: E/AndroidRuntime(270):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:48:41.653: E/AndroidRuntime(270):  at dalvik.system.NativeStart.main(Native Method)
  • 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-05-28T06:09:00+00:00Added an answer on May 28, 2026 at 6:09 am

    Well, one issue I can see is that you’ve got the wrong id when referencing some view, as you’ve passed it a layout identifier:

    View v = (View) findViewById(R.layout.layout_dois);
    

    Your listview just below it will also be null. I’m assuming this is in the “layout_dois” layout?

    Kinda looks like you should just change it so that it reads:

    View v = inflater.inflate(R.layout.layout_dois, null);
    listView = (ListView) v.findViewById(R.id.listView1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to simply get the GalleryView or ListView that is using
I have a ListView and I'm using SimpleCursorAdapter to populate the content, and newView
I have an app using ListView not unlike Explorer does, but when I set
I recently just overhauled my application to use ListView instead of using the classic
I'm using the ListView control (ASP.NET 2008) to show a bunch of lines of
I am using a listView that is being populated from a database. I know
I have filled the listview using the code given below, but I'm unablee to
I am trying to get the ListView by using the findViewById function, yet I
I am trying to get an ASP.NET 3.5 ListView control to select and highlight
I can get the the first row in a ListView item in .NET 3.5

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.