为什么应用程序崩溃了?

时间:2013-08-24 17:44:32

标签: android

我是android的新程序员。我一遍又一遍地检查了我的代码,但是无法让它运行起来!!

public class Playlist extends Activity
{   
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    ListView myListView = (ListView)findViewById(R.string.playlistHolder);
        final ArrayList<String> songslist= new ArrayList<String>();
    final ArrayAdapter<String> aa= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,songslist);
        myListView.setAdapter(aa);

    /*requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);*/;
    setContentView(R.layout.activity_playlist);
    // Show the Up button in the action bar.
    setupActionBar();
    String[] STAR = { "*" };        
    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    cursor = managedQuery(allsongsuri, STAR, selection, null, null);

    if (cursor != null) 
    {
        if (cursor.moveToFirst())
        {
            do 
            {
                //SongName
                String song_name = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

                //SongID
                int song_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media._ID));

                //SongPath
                String fullpath = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));

                //Song's album name
                String album_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));

                //Song's album ID
                int album_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                //Song's artist name
                String artist_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST));

                //Song's artist ID
                int artist_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));

                songslist.add(song_id,song_name);//Adding Song to Arraylist
                aa.notifyDataSetChanged();//Notifying Listview about addition of song.
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
    super.onCreate(savedInstanceState);
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar()
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.playlist, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()) 
    {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

单击播放列表按钮后应用程序崩溃! 我从我的家庭活动中发起了一个新的意图 - &gt;播放列表 如下

playlist.setOnClickListener(new OnClickListener()
{
@Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub
        Intent plist = new Intent(arg0.getContext(), Playlist.class);
        startActivityForResult(plist, 0);
    }
});

1 个答案:

答案 0 :(得分:0)

试试这个

playlist.setOnClickListener(new View.OnClickListener() {
{ 
@Override
public void onClick(View arg0)
{
    // TODO Auto-generated method stub
    Intent plist = new Intent(arg0.getContext(), Playlist.class);
    startActivityForResult(plist, 0);
}
});

将新的 OnClickListener()更改为新的View.OnClickListener()