How do thread of music? - android

i'am trying to develop an app which, when you enter in home screen you can listen a music until you enter in other activity. It Nothing works, and i don't know why...
my code :
public class MainActivity extends Activity {
Button jugar, preferencias, acercade, maximo, salir;
Vibrator v;
Boolean isVibrar,sonido;
MediaPlayer sonidoJuego;
ThreadSonido threadSonido;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_land);
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
isVibrar = sharedPref.getBoolean("vibracion", false);
sonido=sharedPref.getBoolean("musica", true);
if(sonido==true){
threadSonido.start();
}else{
threadSonido.stop();
}
}
public void dentroJuego(View view) {
if (isVibrar==true) {
v.vibrate(100);
}
Intent i = new Intent(MainActivity.this, AngryJuego.class);
startActivity(i);
this.finish();
}
public void salir(View view) {
sonidoJuego.stop();
finish();
}
public void preferencias(View view) {
if (isVibrar==true) {
v.vibrate(100);
}
Intent i = new Intent(MainActivity.this, Preferencias.class);
startActivity(i);
}
public void maximosRegistros(View view) {
if (isVibrar==true) {
v.vibrate(100);
}
Intent i = new Intent(MainActivity.this, MaximosRegistros.class);
startActivity(i);
}
public void lanzarAcercaDe(View view) {
if (isVibrar==true) {
v.vibrate(100);
}
Intent i = new Intent(this, AcercaDe.class);
startActivity(i);
}
public void recargar(){
Intent i= this.getIntent();
finish();
startActivity(i);
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
finish();
}
public class ThreadSonido extends Thread {
#Override
public void run() {
sonidoJuego=MediaPlayer.create(getBaseContext(),R.raw.gaitas);
sonidoJuego.setLooping(true);
sonidoJuego.start();
}
}
}
And the log
05-10 14:08:23.800: E/AndroidRuntime(12283): FATAL EXCEPTION: main
05-10 14:08:23.800: E/AndroidRuntime(12283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sergiochuky.angryfaba/com.example.game.MainActivity}: java.lang.NullPointerException

Related

Android Studio Background MediaPlayer for multible Activities

Can someone tell me why my app keeps crashing? In debug it says "mediaplayer=null"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
play = findViewById(R.id.btn_play);
exit = findViewById(R.id.btn_exit);
settings = findViewById(R.id.btn_settings);
soundbutton = findViewById(R.id.btn_sound_main);
displayPlayername = findViewById(R.id.TextViewPlayerName);
//Display PlayerName - Setup in Settings
if (!Helper.getPlayerName(this).equals("Player"))
displayPlayername.setText(Helper.getPlayerName(this));
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play();
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeApp();
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openSettings();
}
});
// toogle Soundbutton Image and Sound
soundbutton.setOnClickListener(view -> {
if (Helper.getSound(this)) {
soundbutton.setImageResource(R.drawable.volume_off_white);
BackgroundPlayer.mediaPlayer.pause();
Helper.setSound(this, false);
} else {
soundbutton.setImageResource(R.drawable.volume_on_white);
BackgroundPlayer.mediaPlayer.start();
Helper.setSound(this, true);
}
});
}
public void play() {
Intent intent = new Intent(this, LobbyActivity.class);
startActivity(intent);
}
public void closeApp() {
finishAndRemoveTask();
//finish();
//System.exit(0);
}
public void openSettings() {
Intent intent = new Intent(this, SettingActivity.class);
startActivity(intent);
}
#Override
public void onResume() {
super.onResume();
if (Helper.getSound(this)) {
soundbutton.setImageResource(R.drawable.volume_on_white);
BackgroundPlayer.mediaPlayer.start();
} else {
soundbutton.setImageResource(R.drawable.volume_off_white);
}
}
#Override
public void onPause() {
super.onPause();
BackgroundPlayer.mediaPlayer.pause();
}
#Override
public void onDestroy() {
super.onDestroy();
BackgroundPlayer.mediaPlayer.stop();
}
public class BackgroundPlayer extends Application {
public static MediaPlayer mediaPlayer;
#Override
public void onCreate() {
super.onCreate();
setMediaPlayer();
}
public void setMediaPlayer() {
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.backgroundmusic);
mediaPlayer.setLooping(true);
if (Helper.getSound(this)) {
mediaPlayer.start();
} else {
mediaPlayer.start();
mediaPlayer.pause();
}
}
}

How to send data by click back button?

I'm using two activities, MainActivity & SubActivity.
MainActivity.class
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivity(sub);
}
}
}
}
SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
In this case, if I change the text in EditText in SubActivity, I want to send changed text data to MainActivity when back button pressed in SubActivity(It means SubActivity onDestroy()). I don't want to make the second MainActivity like this in SubActivity.
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
startActivity(intent);
}
Is there any way to send text data when SubActivity removed from the activity stack?
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivityForResult(sub , 2);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("name");
textView1.setText(message);
}
}
}
And
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
setResult(2,intent);
}
You need to use startActivityForResult() if you expect to have a return from the started activity.
After that, you'll also need to implement onActivityResult() method on the start activity to actually read the returned data.
Use startActivityForResult instead of startActivity
MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
String name= "a";
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", name);
startActivityForResult(sub,100);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 100) {
String name = data.getStringExtra("name);
text.setText(name);
}
}
}
}
SubActivity:
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
Intent resultIntent = new Intent();
resultIntent.putExtra("name", "your_editext_value");
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can use SharedPreference for storing value and update it or use it anywhere instead of using Intent
like In MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Editor edit=sp.edit();
edit.putString("name","a");
edit.commit();
Intent sub = new Intent(this, SubActivity.class);
startActivity(sub);
}
}
} }
and In SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(sp.getString("name",""));
text.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Editor edit=sp.edit();
edit.putString("name",s.toString);
edit.commit();
} });
}
#Override
public void onBackPressed() {
super.onBackPressed();
}}
To use value from SharedPreference
sp.getString("name","");
For using SharedPreference look at this https://developer.android.com/training/data-storage/shared-preferences#java

when i can pass intent for going another acvity then this type error generate

I can not pass intent another activity. When I do then error is thrown.
My code is:
public class Intro extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
ImageConfig.init(this);
Setting.INSTANCE.initialize(this);
PhotoDeskFragment.initMutiWindow();
if (FolderFragment.getFolderItems() != null) {
photoDeskActivity();
return;
}
if (savedInstanceState == null) {
loading();
}
}
private void loading() {
if (FolderFragment.isItemEmpty()) {
ProtectUtil.getInstance().initialize(this);
HiddenFolderUtil.getInstance().initialize(this);
loadFolderItems();
}
new Handler().postDelayed(new Runnable() {
public void run() {
if (mFuture != null) {
mFuture.waitDone();
}photoDeskActivity();
}
}, ITEM_LOADING_TIME);
}
private void loadFolderItems() {
mFuture = getThreadPool().submit(new LoadFolderItems(), new FutureListener<ArrayList<FolderItem>>() {
#Override
public void onFutureDone(Future<ArrayList<FolderItem>> future) {
FolderFragment.insertFolderItems(future.get());
FolderFragment.runImageLoaderThread(getContentResolver());
}
});
}
public void photoDeskActivity()
{
Intent i=new Intent(Intro.this, PhotoDeskActivity.class);
startActivity(i);
}
}

How can I disable a thread with an onClickListener?

I am working on this splash screen test and implemented a click listener to the layout so it can be skipped by clicking on the screen. The problem is that the intent is called twice, because the Loading thread is not properly interrupted when the screen is clicked. What am I doing wrong and how to avoid this error?
public class SplashScreen extends Activity {
final Thread Loading = new Thread();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.splashscreen);
ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation);
SplashScreenView.setBackgroundResource(R.drawable.flashscreenanimation);
AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground();
SplashScreenAnimation.start();
RelativeLayout OnTouchSkipScreen = (RelativeLayout)findViewById(R.id.SplashScreenView);
OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Loading.interrupt();
Loading.isInterrupted();
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
});
Thread Loading = new Thread() {
public void run() {
try {
sleep(2573);
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
}
};
Loading.start();
}
}
Go for it
public class SplashScreen extends Activity {
Thread Loading ;
boolean flag = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.splashscreen);
ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation);
SplashScreenView.setBackgroundResource(R.drawable.bontactbook);
AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground();
SplashScreenAnimation.start();
RelativeLayout OnTouchSkipScreen = (RelativeLayout) findViewById(R.id.flashscreenanimation);
OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
flag = false;
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
finish();
}
});
Thread Loading = new Thread() {
public void run() {
try {
sleep(5000);
if (flag) {
Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(SplashScreen);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
}
};
Loading.start();
}
}

How do I open a button another activity with another button?

I have made successfully my Imagebutton to open anther activity but the issue is by using the same method on another ImageButton it comes up with an error saying the method is already used in "Main Activity".
public class MainActivity extends ActionBarActivity {
private static ImageButton ImageButton_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickImageButtonListener();
}
public void OnClickImageButtonListener() {
ImageButton_sbm = (ImageButton)findViewById(R.id.imageButton);
ImageButton_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.event");
startActivity(intent);
}
}
);
}
private static ImageButton ImageButton2_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickImageButtonListener();
}
public void OnClickImageButtonListener() {
ImageButton_sbm = (ImageButton)findViewById(R.id.imageButton2);
ImageButton_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.news");
startActivity(intent);
}
}
);
}
How, if anything, can I apply another method for the "saintbedeslytham.saintbedes.news"
Your problem here is that you cannot create two method with the same definition.
You have two:
protected void onCreate(Bundle savedInstanceState);
and two:
public void OnClickImageButtonListener();
You can't have 2 methods with the same definition for 2 simple reasons.Imagine 2 people with the same name, call for their name.
Who will answer?
Who are you calling?
Edit:
So you can:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareClicks();
}
private void prepareClicks() {
findViewById(R.id.imageButton).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.event");
startActivity(intent);
}
}
);
findViewById(R.id.imageButton2).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.news");
startActivity(intent);
}
}
);
}
}
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareClicks();
}
private void prepareClicks() {
findViewById(R.id.imageButton).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.event");
startActivity(intent);
}
}
);
findViewById(R.id.imageButton2).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.news");
startActivity(intent);
}
}
);
}
}
Yup the method worked.
Thanks for the replies,
(Done by Gorcyn)

Categories

Resources