我按回按钮时为什么我的程序崩溃了

时间:2016-08-15 18:16:16

标签: java android eclipse listview android-studio

[这是sd卡][1]

中存在的歌曲的列表视图

****这是我的MainActivity代码****

package com.example.jawad.musicplayer;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements   View.OnClickListener
{

    ListView musicListView;
    ArrayList<File> musicFiles;
    MediaPlayer mediaplayer;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        musicListView = (ListView) findViewById(R.id.musiclistviewId);
         musicFiles = findSongs(Environment.getExternalStorageDirectory());
         CustomListViewAdapter CLV = new CustomListViewAdapter(MainActivity.this,R.layout.list_row,musicFiles);
        musicListView.setAdapter(CLV);
         musicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 startActivity(new Intent(MainActivity.this,PlayerActivity.class).putExtra("music",musicFiles).putExtra("pos",position));
             }
         });

        /*for (int i =0 ;i < music.size();i++)
        {
            toast(music.get(i).getName().toString());
        }*/

    }

    public ArrayList<File> findSongs(File root)
    {
        File[] files = root.listFiles();
        ArrayList<File> tempSongs = new ArrayList<>();
        for(File singlefile : files)
        {
            if(singlefile.isDirectory() && !singlefile.isHidden())
            {
                tempSongs.addAll(findSongs(singlefile));
            }
             else
            {
                if(singlefile.getName().endsWith(".mp3"))
                {
                    tempSongs.add(singlefile);
                }
            }
        }
           return tempSongs;
    }

    public  void toast(String text)
    {
        Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {

    }
}

以下是&#34;玩家活动&#34;

的代码
package com.example.jawad.musicplayer;

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class PlayerActivity extends AppCompatActivity implements View.OnClickListener
{

    Button playButton;
    MediaPlayer mplayer;
    private Button prevButton;
    private  Button nextButton;
    SeekBar sb;
    Thread updateThread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        playButton = (Button) findViewById(R.id.playButtonId);
        prevButton=(Button) findViewById(R.id.prevButtonId);
        nextButton = (Button) findViewById(R.id.nextButtonId);
        sb = (SeekBar) findViewById(R.id.seekbarId);



        playButton.setOnClickListener(this);
        nextButton.setOnClickListener(this);
        prevButton.setOnClickListener(this);

        Bundle extra =  getIntent().getExtras();
        if(extra!=null) {
            ArrayList<File> list = (ArrayList) extra.getParcelableArrayList("music");
            int pos = extra.getInt("pos");
            File file = list.get(pos);
            // Toast.makeText(getApplicationContext(),file.getName().toString(),Toast.LENGTH_LONG).show();

            Uri URI = Uri.parse(file.toString());
            mplayer=MediaPlayer.create(getApplicationContext(),URI);

        }
    }

    @Override
    public void onClick(View v) {
        int id =v.getId();
        switch(id)
        {
            case R.id.playButtonId:
                if(mplayer.isPlaying())
                    pauseMusic();
                 else
                    playMusic();
                break;
            case R.id.prevButtonId:
                break;
            case R.id.nextButtonId:
                break;
        }
    }

    public void playMusic()
    {

        if(mplayer!= null) {
            mplayer.start();
            playButton.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.ic_media_pause));
            updateThread = new Thread() {
                @Override
                public void run()
                {
                    sb.setMax(mplayer.getDuration());
                    int toatalduration = mplayer.getDuration();
                    int currentposition = 0;
                    while (currentposition < toatalduration  && mplayer.isPlaying())
                    {
                        try
                        {
                            sleep(500);
                            currentposition = mplayer.getCurrentPosition();
                            sb.setProgress(currentposition);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }

                    }
                }


            };
        }
          updateThread.start();


    }


    @Override
    public void onBackPressed() {
        updateThread.stop();
        onDestroy();
    }

    public void pauseMusic()
    {
         if (mplayer!=null)
         {
            mplayer.pause();
            playButton.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.ic_media_play));
         }
    }

    @Override
    protected void onDestroy() {
        if(mplayer!=null && mplayer.isPlaying())
        {
            mplayer.stop();
            mplayer.release();
            mplayer = null;
            super.onDestroy();
        }
    }
}

基本上我是Android编程和练习在android中构建媒体播放器的新手。我对Java有很好的控制但是我仍然无法找到这个问题的解决方案,所以欢迎你的帮助。 我也覆盖了onBackPressed()的方法,并且在那个方法中我停止了线程并停止了音乐播放器,但仍然安卓工作室给我错误。

这是我的LogCat:

08-15 14:11:00.721 1429-1429/com.example.jawad.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jawad.musicplayer, PID: 1429
android.util.SuperNotCalledException: Activity {com.example.jawad.musicplayer/com.example.jawad.musicplayer.PlayerActivity} did not call through to super.onDestroy()
 at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3472)
 at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3501)
 at android.app.ActivityThread.access$1400(ActivityThread.java:135)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1249)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5001)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
 at dalvik.system.NativeStart.main(Native Method)
 08-15 14:11:00.721 534-760/system_process W/ActivityManager:   Force finishing activity com.example.jawad.musicplayer/.MainActivity

 [08-15 14:11:00.741   534:  760 D/] HostConnection::get() New Host Connection established 0xb7cbc840, tid 760

0 个答案:

没有答案