So what I am trying to create is an activity that displays an image button. The background for the image button points to an xml in the drawable folder to show the different pictures for on focus and click. That all works fine. I have music in my main activity that is set to loop. By default the image button is set to say Music On. What I want to happen is when the button is clicked the main sound will pause and the button background will change to a different xml drawable layout that says Music Off. When it is clicked again the music will resume where it left off and again switch back to Music On.
One problem I am having is pausing the main sound. Since I'm new to android can a media player variable I reference in my main activity be changed in a different activity? Also, in my options activity I have two if statements under the on click for the image button to check whether the sound is playing or isn't and then will either pause or resume the music depending on which one it is. I am not sure how to do the second if statement but I have the first one that I think might be right.
Sorry that there are a lot of different things I am trying to do, but I tried to break it down. Also, I am getting force closes as of now when I start the optionsActivity and I will put everything underneath including the main activity because that is where I establish the mainSound. Thanks for any help you can give me.
MainActivity:
package com.crazycastles;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private MediaPlayer mainSound;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed //Things to Do
if(mainSound!= null) { mainSound.pause(); mainSound=null; } finish(); return true; } return super.onKeyDown(keyCode, event); }
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainSound = MediaPlayer.create(MainActivity.this, R.raw.mainscreen);
mainSound.setLooping(true);
mainSound.start();
//CREATE BUTTON 1 & SOUND
final MediaPlayer buttonSound = MediaPlayer.create(
MainActivity.this, R.raw.swords);
ImageButton button1 = (ImageButton) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
button1Activity.class));
}
});
ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton);
multiplayerbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
multiplayerbuttonActivity.class));
}
});
ImageButton optionsbutton = (ImageButton) findViewById(R.id.optionsbutton);
optionsbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
optionsActivity.class));
}
});
ImageButton creditbutton = (ImageButton) findViewById(R.id.creditbutton);
creditbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent(MainActivity.this,
creditsActivity.class));
}
});
ImageButton exitbutton = (ImageButton) findViewById(R.id.exitbutton);
exitbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
finish();
mainSound.stop();
System.exit(0);
}
});
//END OF BUTTON1 & SOUND
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
optionsActivity:
package com.crazycastles;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class optionsActivity extends Activity {
/** Called when the activity is first created. */
ImageButton musicbutton, musicbutton2;
private MediaPlayer mainSound;
final MediaPlayer buttonSound = MediaPlayer.create(
optionsActivity.this, R.raw.swords);
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
musicbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mainSound.isPlaying()) {
musicbutton.setBackgroundResource(R.drawable.musicbutton2);
buttonSound.start();
mainSound.pause();
}
}
});
}
}
LogCat:
01-15 16:10:55.059: E/AndroidRuntime(7319): FATAL EXCEPTION: main
01-15 16:10:55.059: E/AndroidRuntime(7319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.crazycastles/com.crazycastles.optionsActivity}: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.access$2500(ActivityThread.java:129)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.os.Looper.loop(Looper.java:143)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.main(ActivityThread.java:4701)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:10:55.059: E/AndroidRuntime(7319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:10:55.059: E/AndroidRuntime(7319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:10:55.059: E/AndroidRuntime(7319): at dalvik.system.NativeStart.main(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): Caused by: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.media.MediaPlayer.create(MediaPlayer.java:641)
01-15 16:10:55.059: E/AndroidRuntime(7319): at com.crazycastles.optionsActivity.<init>(optionsActivity.java:17)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.Class.newInstanceImpl(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): at java.lang.Class.newInstance(Class.java:1429)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 16:10:55.059: E/AndroidRuntime(7319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
I also have developed a music player in Android
One problem I am having is pausing the main sound. Since I'm new to
android can a media player variable I reference in my main activity be
changed in a different activity?
I could say it yes, if you declare it as a static Object
Also, in my options activity I have two if statements under the on
click for the image button to check whether the sound is playing or
isn't and then will either pause or resume the music depending on
which one it is. I am not sure how to do the second if statement but I
have the first one that I think might be right.
I think you have to look at Android Media Player lifecycle, you could re-use your object but there is some conditions : http://developer.android.com/reference/android/media/MediaPlayer.html
Since I'm new to android can a media player variable I reference in my main activity be changed in a different activity?
No. If you create a MediaPlayer in an activity, it should only be used while that activity is in the foreground. Your MediaPlayer most likely should be managed by a Service, if you are planning on it continuing to play once the user has left the activity.
Related
I have this problem I want to pass a raw from Activity 1 to be played in Activity 2
Here is my code
Activity 1
package com.ze.zeggar.tewt;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
int ThePlayedSound= R.raw.waterdrop;
Intent MyIntent= new Intent(this, MainActivity.class);
MyIntent.putExtra("key",ThePlayedSound);
startActivity(MyIntent);
}
}
Activity 2
package com.ze.zeggar.tewt;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
SoundPool Sound_Pool_thing;
int Click;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent MyIntent=getIntent();
int ThePlayedSound= MyIntent.getIntExtra("key", 0);
Sound_Pool_thing= new SoundPool(1, AudioManager.STREAM_MUSIC,0);
Click = Sound_Pool_thing.load(this,ThePlayedSound , 1);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sound_Pool_thing.play(Click,1,1,1,0,1);
}
});
}
}
and when I try to open the app I got "Unfortunately , My_App has stopped"
The app crashes
and here's the new crash Log after suggestions:
06-06 10:49:09.069 3424-3424/com.ze.zeggar.tewt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ze.zeggar.tewt, PID: 3424
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ze.zeggar.tewt/com.ze.zeggar.tewt.MainActivity}:
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1269)
at android.content.res.Resources.openRawResourceFd(Resources.java:1227)
at android.media.SoundPool$SoundPoolImpl.load(SoundPool.java:566)
at android.media.SoundPool.load(SoundPool.java:229)
at com.ze.zeggar.tewt.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Any idea ?
Ur getting resource not found exception since your not opening the raw type Resources properly
Try getting the raw audio file from resources like this:
int ThePlayedSound = this.getResources().getIdentifier("waterdrop", "raw", getPackageName());
Intent MyIntent= new Intent(this, MainActivity.class);
MyIntent.putExtra("key",ThePlayedSound);
startActivity(MyIntent);
What about using the MediaPlayer class?
Check this code below:
MainActivity class (or whatever class you want to start the new activity from)
Intent intent = new Intent(this,Main2Activity.class);
intent.putExtra("key",R.raw.waterdrop);
startActivity(intent);
Main2Activity (class that will play the audio)
public class Main2Activity extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitiy_main2);
int soundId = getIntent().getIntExtra("key",0);
final MediaPlayer mPlayer =new MediaPlayer();
mPlayer.setAudioSessionId(soundId);
mPlayer.setLooping(true);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.start();
}
});
}
}
This code worked for me.
If it says that that specific constructor doesnt work try the below one:
final MediaPlayer mPlayer =new MediaPlayer(this,soundId);
Hope it helps you out!
Today is my first day with java. I can not figure out the reason of this error.
MainActivity.java is like this below
package com.example.ragefacefolks;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button bMain = (Button) findViewById(R.id.button1);
#Override
public void registerForContextMenu(View view) {
// TODO Auto-generated method stub
super.registerForContextMenu(bMain);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
}
wont be hard for you guys to know its reaon. Need a little help.
log cat is showing these results
09-27 07:59:24.677: D/AndroidRuntime(555): Shutting down VM
09-27 07:59:24.677: W/dalvikvm(555): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-27 07:59:24.707: E/AndroidRuntime(555): FATAL EXCEPTION: main
09-27 07:59:24.707: E/AndroidRuntime(555): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ragefacefolks/com.example.ragefacefolks.MainActivity}: java.lang.NullPointerException
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.os.Looper.loop(Looper.java:123)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-27 07:59:24.707: E/AndroidRuntime(555): at java.lang.reflect.Method.invokeNative(Native Method)
This
Button bMain = (Button) findViewById(R.id.button1);
Should be in onCreate after setContentView. You initialize it outside of any method
private Button bMain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bMain = (Button) findViewById(R.id.button1);
}
You cannot call:
Button bMain = (Button) findViewById(R.id.button1);
in the class like that (specifically, cannot be called before onCreate).
Move this code to onCreate.
Remove line
Button bMain = (Button) findViewById(R.id.button1);
and change onCreate method.-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bMain = (Button) findViewById(R.id.button1);
registerForContextMenu(bMain);
}
There's no need to override registerForContextMenu method.
I am doing an android app and i was thinking of creating a theme option. Well all I was thinking of doing was to allow the user to click on a image button which represented a theme. And when he clicks on it I start a new activity i.e i direct him to the home page. Also i have created a few integer variables which are set to 1 when the user clicks on a button.
Then in the other classes all i do is check if the variables are 1 or not and depending on that i apply the theme. By theme i mean i just change the background wallpaper. But this is not working. I mean the code works but if use an if loop to check the variable values and then apply the effects it causes an error.
Here's the complete code:
package com.example.themetest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener{
ImageButton ib1;
ImageButton ib2;
int water=0;
int fire=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib1 = (ImageButton) findViewById(R.id.imageButton1);
ib2 = (ImageButton) findViewById(R.id.imageButton2);
ib1.setOnClickListener(this);
ib2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imageButton1:
water = 1;
Intent wi = new Intent("com.example.themetest.THEME");
startActivity(wi);
break;
case R.id.imageButton2:
fire = 1;
Intent fi = new Intent("com.example.themetest.THEME");
startActivity(fi);
break;
}
}
}
Here's the other class where i check which variable is set to 1 and apply the effect.
package com.example.themetest;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
public class Theme extends Activity{
MainActivity main;
Resources res;
Drawable drawable;
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theme);
if(main.water==1){
res = getResources();
drawable = res.getDrawable(R.drawable.water_theme);
linearLayout = (LinearLayout)findViewById(R.id.ll);
linearLayout.setBackgroundDrawable(drawable);
}
else if(main.fire==1){
res = getResources();
drawable = res.getDrawable(R.drawable.fire_theme);
linearLayout = (LinearLayout)findViewById(R.id.ll);
linearLayout.setBackgroundDrawable(drawable);
}
else{
res = getResources();
drawable = res.getDrawable(R.drawable.ic_launcher);
linearLayout = (LinearLayout)findViewById(R.id.ll);
linearLayout.setBackgroundDrawable(drawable);
}
}
}
I can change the wallpaper without using the if loop, but i want to do it this way which is not working. Can anyone please tell me why?
Log cat:
01-15 12:08:23.339: D/dalvikvm(273): GC_EXTERNAL_ALLOC freed 767 objects / 55936 bytes in 235ms
01-15 12:08:25.539: D/AndroidRuntime(273): Shutting down VM
01-15 12:08:25.539: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 12:08:25.559: E/AndroidRuntime(273): FATAL EXCEPTION: main
01-15 12:08:25.559: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.themetest/com.example.themetest.Theme}: java.lang.NullPointerException
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 12:08:25.559: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 12:08:25.559: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 12:08:25.559: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 12:08:25.559: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 12:08:25.559: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method)
01-15 12:08:25.559: E/AndroidRuntime(273): Caused by: java.lang.NullPointerException
01-15 12:08:25.559: E/AndroidRuntime(273): at com.example.themetest.Theme.onCreate(Theme.java:29)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-15 12:08:25.559: E/AndroidRuntime(273): ... 11 more
01-15 12:08:31.089: I/Process(273): Sending signal. PID: 273 SIG: 9
I think the better way to do this is pass the name or code of the theme user selected along with the intent.
This might help: Passing data through Intent and receiving it
You cannot access other activities variables this way, a better way (for example) is to use a constant class..
public class Constants {
public static int water=0;
public staticint fire=0;
}
MainActivity:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imageButton1:
Constants.water = 1;
Intent wi = new Intent("com.example.themetest.THEME");
startActivity(wi);
break;
case R.id.imageButton2:
Constants.fire = 1;
Intent fi = new Intent("com.example.themetest.THEME");
startActivity(fi);
break;
}
}
Theme:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theme);
if(Constants.water==1){
res = getResources();
drawable = res.getDrawable(R.drawable.water_theme);
linearLayout = (LinearLayout)findViewById(R.id.ll);
linearLayout.setBackgroundDrawable(drawable);
}
else if(Constants.fire==1){
res = getResources();
drawable = res.getDrawable(R.drawable.fire_theme);
linearLayout = (LinearLayout)findViewById(R.id.ll);
linearLayout.setBackgroundDrawable(drawable);
}
else{
res = getResources();
drawable = res.getDrawable(R.drawable.ic_launcher);
linearLayout = (LinearLayout)findViewById(R.id.ll);
linearLayout.setBackgroundDrawable(drawable);
}
}
I am developing an app for android that has to access another class, but i don't know why it doesn't work.
When run the App in android 2.3.3 it force closes, and I don't understand why. I think that the method is correct.
Log in the force close the phone android:
> app_vercode:1
device_model:u8800
build_version:111180
condition:1
processName:beta.tester
pid:13277
uid:10088
tag:null
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at beta.tester.BetaTesterActivity.onCreate(BetaTesterActivity.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more
Detail logs:
EDIT: This code already is correctly.
The code:
class BetaTesterActivity:
package beta.tester;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BetaTesterActivity extends Activity {
public TextView text1;
private teste cmd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (TextView) findViewById(R.id.text1);
//Start the function
cmd = new teste();
cmd.start(this);
}
}
class teste:
package beta.tester;
public class teste {
//Function that I will start
public void start(BetaTesterActivity zav){
zav.text1.setText("Hello");
}
//
}
In class teste, you are creating a new BetaTesterActivity, which is useless. You need to use the instance created by the framework. Change your class teste to this:
public class teste {
//Function that I will start
public void start(BetaTesterActivity zav){
zav.text1.setText("Hello");
}
}
Then in the onCreate method of your activity class, you need to initialize cmd and then call start like this:
cmd.start(this);
I'm having some errors with my custom recording application using android. I saw most of this code from a book called PRO Android Media. I developed the app using sdk "8" version. However, I am testing the phone on my htc evo 4g which just got updated to gingerbread. So I dont know how much that would have changed. Anyways my app still wasnt working when i tested it on 2.2 version either.
Here is the code below
package com.apapa.vrsixty;
import java.io.File;
import java.io.IOException;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class record extends Activity implements OnClickListener, SurfaceHolder.Callback{
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording=false;
public static final String TAG = "VIDEOCAPTURE";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
recorder = new MediaRecorder();// Instantiate our media recording object
initRecorder();
setContentView(R.layout.view);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_view);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);// make the surface view clickable
cameraView.setOnClickListener((OnClickListener) this);// onClicklistener to be called when the surface view is clicked
}
private void initRecorder() {// this takes care of all the mediarecorder settings
File OutputFile = new File(Environment.getExternalStorageDirectory().getPath());
String video= "/DCIM/100MEDIA/Video";
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
//recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// default microphone to be used for audio
// recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// default camera to be used for video capture.
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// generally used also includes h264 and best for flash
// recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //well known video codec used by many including for flash
//recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// typically amr_nb is the only codec for mobile phones so...
//recorder.setVideoFrameRate(15);// typically 12-15 best for normal use. For 1080p usually 30fms is used.
// recorder.setVideoSize(720,480);// best size for resolution.
//recorder.setMaxFileSize(10000000);
recorder.setOutputFile(OutputFile.getAbsolutePath()+video+".3gp");
//recorder.setVideoEncodingBitRate(256000);//
//recorder.setAudioEncodingBitRate(8000);
recorder.setMaxDuration(600000);
}
/*if(record.setMaxDuration>60000){
recorder.stop();
MediaRecorder.OnInfoListener;
Toast display = Toast.makeText(this, "You have exceeded the record time", Toast.LENGTH_SHORT);// toast shows a display of little sorts
display.show();
return true;
}*/
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
public void onClick(View v) {
if (recording) {
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
Toast display = Toast.makeText(this, "Stopped Recording", Toast.LENGTH_SHORT);// toast shows a display of little sorts
display.show();
} else {
recorder.start();
Log.v(TAG,"Recording Started");
recording = true;
}
}
public void surfaceCreated(SurfaceHolder holder) {
initRecorder();
Log.v(TAG,"surfaceCreated");
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
finish();
}
Its bringing up these errors.
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apapa.vrsixty/com.apapa.vrsixty.record}: java.lang.IllegalStateException
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.os.Looper.loop(Looper.java:143)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.ActivityThread.main(ActivityThread.java:4263)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at java.lang.reflect.Method.invoke(Method.java:507)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at dalvik.system.NativeStart.main(Native Method)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): Caused by: java.lang.IllegalStateException
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.media.MediaRecorder.setOutputFormat(Native Method)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.media.MediaRecorder.setProfile(MediaRecorder.java:293)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at com.apapa.vrsixty.record.initRecorder(record.java:67)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at com.apapa.vrsixty.record.onCreate(record.java:50)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
Please any help will suffice please. I just cant wait to see an actually recording in stead of "The application has stopped unexpectedly."
Update:
Well thanks, recorder.reset(); worked for me but I am having an issue when I start the recorder. I have to click on my phone before it actually shows the preview. Also, how do i make the recorder show numbers in a countdown when the video starts recording? The video doesn't save to my sd card even though i include
recorder.OutPutFile("/sdcard/video.mp4");
And I have the permission WRITE_EXTERNAL_STORAGE in my manifest file.
Another question is how do i save the video in an incrememntal manner. For example, the first video that i record saves as "video1", the second video is "video2" and so on and so forth. I will appreciate any help that I can get with this issue thank you.
You also need to make sure you can write to the SD Card:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Finally, you MUST set the audio and video source. Try replacing your initRecorder method with the one from the book to see if you get further:
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/videocapture_example.mp4");
recorder.setMaxDuration(50000); // 50 seconds
recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}
You didn't know the function setProfile().
The function setProfile() do all of the things below:
setProfile after setOutputFormat.
public void setProfile(CamcorderProfile profile) {
setOutputFormat(profile.fileFormat);
setVideoFrameRate(profile.videoFrameRate);
setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
setVideoEncodingBitRate(profile.videoBitRate);
setAudioEncodingBitRate(profile.audioBitRate);
setAudioChannels(profile.audioChannels);
setAudioSamplingRate(profile.audioSampleRate);
setVideoEncoder(profile.videoCodec);
setAudioEncoder(profile.audioCodec);
}
Do you have the permission set in AndroidManifest.xml?
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
Also I have had issues like this before try calling:
recorder.reset();
at the start of your initRecorder() method.