2014-07-02 33 views
0

所以我相當新的Android。我只是想解釋一下: 我正在嘗試製作一個小型的便攜式音樂應用程序,僅用於我的手機,並測試我的技能/查看所學知識。我的主要活動設置是用4個按鈕中的1個按鈕選擇一首歌曲,然後使用按鈕啓動另一個活動,以暫停,恢復並返回歌曲選擇屏幕(MainActivity)。我試圖讓後退按鈕釋放玩家並完成活動,我嘗試了許多不同的東西,但似乎沒有任何工作;應用程序由於某種異常而關閉(常見的是NullPointerException)。Android - 如何在單擊另一課中的按鈕後重新啓動MainActivity?

所以這裏的MainActivity:

package me.lemmy.portablemusic.app; 

import android.media.MediaPlayer; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends ActionBarActivity { 

Button wreckingBall, happy, lig, ligMulti; 
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED"); 
public static MediaPlayer player; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    //set buttons 
    wreckingBall = (Button) findViewById(R.id.song_wreck); 
    happy = (Button) findViewById(R.id.song_happy); 
    lig = (Button) findViewById(R.id.song_lig); 
    ligMulti = (Button) findViewById(R.id.song_lig_multi); 

    //listeners 
    wreckingBall.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      startActivity(songPickedActivity); 
      player = MediaPlayer.create(MainActivity.this, R.raw.wrecking_ball); 
      player.start(); 
     } 
    }); 

    happy.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      startActivity(songPickedActivity); 
      player = MediaPlayer.create(MainActivity.this, R.raw.happy); 
      player.start(); 
     } 
    }); 

    lig.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      startActivity(songPickedActivity); 
      player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go); 
      player.start(); 
     } 
    }); 

    ligMulti.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      startActivity(songPickedActivity); 
      player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go_multi); 
      player.start(); 
     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public static MediaPlayer getPlayer(){ 
    return player; 
} 

} 

,這裏是我的SongPicked活動:

package me.lemmy.portablemusic.app; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.media.MediaPlayer; 
import android.widget.TextView; 

/** 
* Created by Lemuel on 6/20/14. 
*/ 
public class SongPicked extends Activity { 

TextView text; 
Button pause, resume, back; 
MediaPlayer player; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_songpicked); 

    text = (TextView) findViewById(R.id.tvPlaying); 
    pause = (Button) findViewById(R.id.buttonPause); 
    resume = (Button) findViewById(R.id.buttonResume); 
    back = (Button) findViewById(R.id.buttonBack); 

    player = MainActivity.getPlayer(); 

    pause.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      player.pause(); 
     } 
    }); 

    resume.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      player.start(); 
     } 
    }); 

    back.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view){ 
      player.release(); 
     } 
    }); 

} 

@Override 
protected void onPause(){ 
    super.onPause(); 
    finish(); 
} 
} 

任何幫助表示感謝,謝謝! P.S.我知道我不能在我的應用中使用受版權保護的音樂,這只是一個測試。

+2

請發佈堆棧跟蹤。 –

+0

好吧,給我一點時間。我也可能需要在這裏更新代碼。 – Lemmy301

+0

@ Code-Apprentice我實際上已經開始工作了,但爲了確保我在這裏沒有做任何不好的事情,請點擊這裏:http://pastebin.com/DpgeFzuc – Lemmy301

回答

2

我建議首先確保活動在AndroidManifest文件中。二是你的意圖,這一變化:

Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");

到:

Intent songPickedActivity = new Intent(this, SongPicked.class); 

回去MainActivity時做得一樣好上songPickedActivity。您可以使用putExtra將數據發送到下一個意圖。欲瞭解更多信息,請點擊這裏link

相關問題