参数保持返回空

时间:2015-05-22 08:23:23

标签: android arguments argument-passing

您好我已经有好几个月的问题,所以请有人帮忙,我是开发Android应用程序的初学者,这就是为什么我无法解决这个问题。

问题: 开发音乐播放器,问题在于播放列表及其ID, 当我在参数下传递PlaylistId时它没有返回任何内容,因此屏幕保持白色。现在我已经尝试了很多东西来解决这个问题,我设法得到了问题与参数(适配器很好,布局很好,其他一切都很好)所以请知道它需要一些时间和工作请帮助。

NavUtils:

public final class NavUtils {

public static void openSearch(final Activity activity, final String query) {
    final Bundle bundle = new Bundle();
    final Intent intent = new Intent(activity, Search.class);
    intent.putExtra(SearchManager.QUERY, query);
    intent.putExtras(bundle);
    activity.startActivity(intent);
}
    /**
     * Opens the profile of an artist.
     * 
     * @param context The {@link android.app.Activity} to use.
     * @param artistName The name of the artist
     */
    public static void openArtistProfile(final Activity context,
            final String artistName) {

        // Create a new bundle to transfer the artist info
        final Bundle bundle = new Bundle();
        bundle.putLong(Config.ID, Utils.getIdForArtist(context, artistName));
        bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Artists.CONTENT_TYPE);
        bundle.putString(Config.ARTIST_NAME, artistName);

        // Create the intent to launch the profile activity
        final Intent intent = new Intent(context, Profile.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

/**
 * Opens the profile of an album.
 *
 * @param context The {@link android.app.Activity} to use.
 * @param albumName The name of the album
 * @param artistName The name of the album artist
 * @param albumId The id of the album
 */
public static void openAlbumProfile(final Activity context,
                                    final String albumName, final String artistName, final long albumId) {

    // Create a new bundle to transfer the album info
    final Bundle bundle = new Bundle();
    bundle.putString(Config.ALBUM_YEAR, Utils.getReleaseDateForAlbum(context, albumId));
    bundle.putString(Config.ARTIST_NAME, artistName);
    bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Albums.CONTENT_TYPE);
    bundle.putLong(Config.ID, albumId);
    bundle.putString(Config.NAME, albumName);

    // Create the intent to launch the profile activity
    final Intent intent = new Intent(context, Profile.class);
    intent.putExtras(bundle);
    context.startActivity(intent);
}

public static void openPlaylistProfile(final Activity context,
                                    final String playlistName, final long playlistId) {

    // Create a new bundle to transfer the album info
    final Bundle bundle = new Bundle();


    bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Playlists.CONTENT_TYPE);
    bundle.putLong(Config.ID, playlistId);
    bundle.putString(Config.NAME, playlistName);

    // Create the intent to launch the profile activity
    final Intent intent = new Intent(context, Profile.class);
    intent.putExtras(bundle);
    context.startActivity(intent);
}



}

PlaylistFragment:

public class PlaylistFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,OnItemClickListener {

private PlaylistsAdapter mAdapter;
GridView gridview;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_playlist, container, false);
    mAdapter = new PlaylistsAdapter(getActivity(), null);
    gridview = (GridView) myFragmentView.findViewById(R.id.playlistGrid);

    getLoaderManager().initLoader(0, null, this);
    return myFragmentView;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    gridview.setAdapter(mAdapter);
    gridview.setOnItemClickListener(this);

}


@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new CursorLoader(getActivity(), MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
            new String[] {
                    /* 0 */
                    BaseColumns._ID,
                    /* 1 */
                    MediaStore.Audio.PlaylistsColumns.NAME
            }, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final Bundle bundle = new Bundle();
    Cursor cursor = mAdapter.getCursor();

    NavUtils.openPlaylistProfile(getActivity(),cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.NAME)),cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID)));
}
  }

PlaylistSong:

public class PlaylistSong extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,AdapterView.OnItemClickListener {
private SongAdapter mAdapter;
ListView listView;
long playlistID;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_song, container, false);
    mAdapter = new SongAdapter(getActivity(), null);
    listView = (ListView) myFragmentView.findViewById(R.id.songlist);
    final Bundle arguments = getArguments();

        getLoaderManager().initLoader(0, null, this);  /// if i set the second parameters as arguments instead of null it returns NullPointer

    playlistID = arguments.getLong(Config.ID);

    return myFragmentView;
}

/**
 * {@inheritDoc}
 */
@Override
public void onSaveInstanceState(final Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putAll(getArguments() != null ? getArguments() : new Bundle());
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    listView.setAdapter(mAdapter);
    listView.setOnItemClickListener(this);
    listView.setFastScrollEnabled(true);

}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    final StringBuilder mSelection = new StringBuilder();
    mSelection.append(MediaStore.Audio.AudioColumns.IS_MUSIC + "=1");
    mSelection.append(" AND " + MediaStore.Audio.AudioColumns.TITLE + " != ''");
    return new CursorLoader(getActivity(),MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID),
            new String[] {
                    /* 0 */
                    MediaStore.Audio.Playlists.Members._ID,
                    /* 1 */
                    MediaStore.Audio.Playlists.Members.AUDIO_ID,
                    /* 2 */
                    MediaStore.Audio.AudioColumns.TITLE,
                    /* 3 */
                    MediaStore.Audio.AudioColumns.ARTIST,
                    /* 4 */
                    MediaStore.Audio.AudioColumns.ALBUM,
                    /* 5 */
                    MediaStore.Audio.AudioColumns.DURATION
            }, mSelection.toString(), null,
            MediaStore.Audio.AudioColumns.TITLE);
}
  /*   if i replace the above code with the one below it displays all the songs fine 
 String select = null;
    final StringBuilder mSelection = new StringBuilder();
    mSelection.append(MediaStore.Audio.AudioColumns.IS_MUSIC + "=1");
mSelection.append(" AND " + MediaStore.Audio.AudioColumns.TITLE + " != ''");
   return new CursorLoader(getActivity(),MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] {
            /* 0 */
                    BaseColumns._ID,
            /* 1 */
                    MediaStore.Audio.AudioColumns.TITLE,
            /* 2 */
                    MediaStore.Audio.AudioColumns.ARTIST,
            /* 3 */
                    MediaStore.Audio.AudioColumns.ALBUM,
            /* 4 */
                    MediaStore.Audio.AudioColumns.DURATION,
            /*5*/
                    MediaStore.Audio.AudioColumns.ALBUM_ID
            }, mSelection.toString(), null,
            MediaStore.Audio.AudioColumns.TITLE);
 */

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

        mAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

        mAdapter.swapCursor(null);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}
}

资料:

public class Profile extends FragmentActivity {


/**
 * The Bundle to pass into the Fragments
 */
private Bundle mArguments;

public static   long playlistId;

/**
 * MIME type of the profile
 */
private String mType;
public static long ID;
private long IDLong;
PagerAdapter mPagerAdapter;
/**
 * Artist name passed into the class
 */
private String mArtistName;
private String mPlaylistName;


/**
 * The main profile title
 */
private String mProfileName;
private Drawable ActionBarBackgroundDrawable;


@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Temporay until I can work out a nice landscape layout
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_profile);

    ///TODO FIX PLAYLIST ARGUMENTS


    // Initialize the Bundle
    mArguments = savedInstanceState != null ? savedInstanceState : getIntent().getExtras();
    // Get the MIME type
    mType = mArguments.getString(Config.MIME_TYPE);
    ID = mArguments.getLong(Config.ID);
    // Initialize the pager adapter
    mPagerAdapter = new PagerAdapter(this);



    // Get the profile title
    mProfileName = mArguments.getString(Config.NAME);
    // Get the artist name
    if (isArtist() || isAlbum()) {
        mArtistName = mArguments.getString(Config.ARTIST_NAME);
    }

   displayView();





}


 @Override
    public void onBackPressed() {
       // super.onBackPressed();

        Intent start = new Intent(this,Base.class);
        startActivity(start);
        finish();
    }
@Override
protected void onSaveInstanceState(final Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putAll(mArguments);
}
private void displayView() {
    // update the main content by replacing fragments
    Fragment fragment = null ;



    if(isAlbum()){

            Toast.makeText(getBaseContext(), "isAlbum", Toast.LENGTH_LONG).show();
            Bundle bundle=new Bundle();
            bundle.putLong(Config.ID, ID);



             fragment=new AlbumSong();

            fragment.setArguments(bundle);



            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.profileContainer, fragment).commit();
            Toast.makeText(getBaseContext(), "Fragment Chnaged Succesfully", Toast.LENGTH_LONG).show();
            // Action bar title = album name
            setTitle(mProfileName);
}else{
    if (isPlaylist()) {
        // Add the carousel images


        Toast.makeText(getBaseContext(), "isPlaylist", Toast.LENGTH_LONG).show();
        Bundle bundle=new Bundle();
        bundle.putLong(Config.ID, ID);



        fragment=new PlaylistSong();

        fragment.setArguments(bundle);



        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.profileContainer, fragment).commit();
        Toast.makeText(getBaseContext(), "Fragment Changed Successfully", Toast.LENGTH_LONG).show();
        // Action bar title = album name
        setTitle(mProfileName);

    }
}}



private final boolean isPlaylist() {
    return mType.equals(MediaStore.Audio.Playlists.CONTENT_TYPE);
}
private final boolean isArtist() {
    return mType.equals(MediaStore.Audio.Artists.CONTENT_TYPE);
}

/**
 * @return True if the MIME type is vnd.android.cursor.dir/albums, false
 *         otherwise.
 */
private final boolean isAlbum() {
    return mType.equals(MediaStore.Audio.Albums.CONTENT_TYPE);
}
 }

如果有其他方法可以做到这一点我就是全耳朵。代码中的新更新请阅读评论,现在当我设置PlaylistSong以显示它工作正常的所有歌曲。现在,当我在项目点击上设置PlayPlaylist方法时,它需要PlaylistId工作猜测是什么,它播放播放列表中的歌曲,所以我认为问题在于显示此特定播放列表的歌曲。所以当播放播放列表时显示播放列表时播放列表ID为空。请帮助并且如果有人需要更多代码告诉我。

PlayPlaylist():

 public static void playPlaylist(final Context context, final long playlistId) {
    final long[] playlistList = getSongListForPlaylist(context, playlistId);
    if (playlistList != null) {
        playAll(context, playlistList, -1, false);
    }
}

getSongListForPlaylist():

 public static final long[] getSongListForPlaylist(final Context context, final long playlistId) {
    final String[] projection = new String[] {
            Playlists.Members.AUDIO_ID
    };
    Cursor cursor = context.getContentResolver().query(
            Playlists.Members.getContentUri("external",
                    Long.valueOf(playlistId)), projection, null, null,
            Playlists.Members.DEFAULT_SORT_ORDER);

    if (cursor != null) {
        final long[] list = getSongListForCursor(cursor);
        cursor.close();
        cursor = null;
        return list;
    }
    return sEmptyList;
}

0 个答案:

没有答案