2013-08-24 116 views
-2

我是一名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); 
} 

只要我点击播放列表按钮,应用程序就会崩溃! 我从我家活动发起一个新的意图 - >播放列表 据如下

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); 
    } 
}); 
+5

发布您的Logcat错误。 – Rahul

+0

您是否在AndroidManifest中声明了“Playlist”活动? – Henry

+0

那么它有一个播放列表 – user2714061

回答

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()

+0

这对它没有任何影响! – user2714061

+0

把你的Logcat。 – Rahul

+0

好吧它说08-24 23:23:14.720:E/SurfaceTexture(120):[RecentsPanel] drainQueueLocked:SurfaceTexture已被放弃! – user2714061

相关问题