I have a Popup window (non-Activity class) that have some buttons. one of them (btn_audio), play or pause sound and it works like a charm.
Now I want to call two method, play() and pause() in my HomeActivity and inside of onStop() method in all of my Activities. it doesn't work, call play() and pause() in other Activity's occur NullPointException.
public class Popup_Menu extends Activity implements OnClickListener {
Button btn_audio;
public static MediaPlayer player; String play_or_pause;
int num_ply, tim_pos; int [] resID;// Audio
SharedPreferences sp;
void showPopup(final Activity context) {
this.context=context;
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup_layout_menu, viewGroup);
sp = context.getSharedPreferences("text", MODE_PRIVATE);
btn_audio = (Button) layout.findViewById(R.id.btn_audio); // audio
layout.findViewById(R.id.btn_audio).setOnClickListener(this);
#Override
public void onClick(View whichButtonIsClicked) {
switch (whichButtonIsClicked.getId()) {
case R.id.btn_audio:
if (play_or_pause.equals("play")) {
pause();
} else {
play(context);
}
sp.edit().putString("play_or_pause", play_or_pause).commit(); // save current stat
} ////// close of showPopup()
//define play and pause methods
public void pause() {
player.pause();
btn_audio.setBackgroundResource(R.drawable.icon_audio_pause);
play_or_pause="pause"; // current state is 'pause'
}
public void play(final Context context) {
if(num_ply==9){num_ply=0; }
resID = new int []{ R.raw.tarane,R.raw.toofan,R.raw.shane,R.raw.kharazmi,R.raw.golhaye_khofte,R.raw.rang,R.raw.naghmeh,R.raw.dar_rahe_to,R.raw.emperor};
player=MediaPlayer.create(context,resID[num_ply]);
player.seekTo(tim_pos);
player.start();
btn_audio.setBackgroundResource(R.drawable.icon_audio_play);
play_or_pause="play"; // current state is 'play'
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
tim_pos=0; num_ply++; play(context);
}
});
}
}
and my HomeActivity :
public class HomeActivity extends Activity implements OnClickListener {
Popup_Menu ppp;
#Override
protected void onCreate..............
ppp = new Popup_Menu();
SharedPreferences sp = getSharedPreferences("text", MODE_PRIVATE);
if(sp.getString("play_or_pause", "play").equals("pause")){
ppp.play(getApplicationContext()); // ERROR
} // audio
} /// close of onCreate
#Override
public void onStop() {
super.onStop();
if(play_or_pause.equals("play")){
ppp.tim_pos= ppp.player.getCurrentP
Ok, I decided to use service to resolve this problem and it works well for play(). but when playing, still i can't call pause() method from SoundService because player object is null. I think by creating an object, from SoundService class, (ss in Popup_Menu class) , default constructor put player value to null (when playing and player object wasn't null). and calling player.pause occur error.
How PLEASE can i pause this one_week_friend!!!
public class SoundService extends Service {
MediaPlayer player; int num_ply, tim_pos; int [] resID;
public int onStartCommand(Intent intent, int flags, int startId) {
resID = new int []{ R.raw.tarane,R.raw.toofan,R.raw.shane,R.raw.kharazmi,
R.raw.golhaye_khofte,R.raw.rang,R.raw.naghmeh,R.raw.dar_rahe_to,R.raw.emperor };
player=MediaPlayer.create(this,resID[num_ply]);
play();
return Service.START_FLAG_REDELIVERY;
}
public void play() {
if(num_ply==9){num_ply=0; }
player.seekTo(tim_pos);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
tim_pos=0; num_ply++; play();
}
});
}
public void resume(){
player.seekTo(tim_pos);
player.start();
}
public void pause() {
tim_pos= player.getCurrentPosition(); ////// ERROR
player.pause(); ////// ERROR
num_ply++;
}
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
And my Popup_Menu class:
#Override
public void onClick(View whichButtonIsClicked) {
switch (whichButtonIsClicked.getId()) {
case R.id.btn_audio:
if (play_or_pause.equals("play")) {
btn_audio.setBackgroundResource(R.drawable.icon_audio_pause);
play_or_pause="pause"; // current state is 'pause'
SoundService ss = new SoundService();
ss.pause(); ////// ERROR
} else {
btn_audio.setBackgroundResource(R.drawable.icon_audio_play);
play_or_pause="play"; // current state is 'play'
Intent sound_Intent = new Intent(context , SoundService.class); // start service
context.startService(sound_Intent);
}
sp.edit().putString("play_or_pause", play_or_pause).commit(); // save current state
break;
LogCat:
08-05 01:03:31.440: E/AndroidRuntime(25356): FATAL EXCEPTION: main
08-05 01:03:31.440: E/AndroidRuntime(25356): java.lang.NullPointerException
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.codegostarNiloo.negar.SoundService.pause(SoundService.java:47)
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.codegostarNiloo.negar.Popup_Menu.onClick(Popup_Menu.java:234)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.view.View.performClick(View.java:4209)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.view.View$PerformClick.run(View.java:17457)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.os.Handler.handleCallback(Handler.java:725)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.os.Looper.loop(Looper.java:153)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.app.ActivityThread.main(ActivityThread.java:5341)
08-05 01:03:31.440: E/AndroidRuntime(25356): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 01:03:31.440: E/AndroidRuntime(25356): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
08-05 01:03:31.440: E/AndroidRuntime(25356): at dalvik.system.NativeStart.main(Native Method)
08-05 01:03:32.066: D/dalvikvm(25356): threadid=11: interp stack at 0x5f1c9000
First, off, don't extend an Activity if it's a non Activity, as you claim, then passing a Context is preferred over an Activity
Secondly, there is a class called DialogFragment that is intended to be used as a popup dialog
I think you are looking for something like the following, though
public class Popup_Menu implements OnClickListener {
Button btn_audio;
public MediaPlayer player;
String play_or_pause;
int num_ply, tim_pos;
int [] resID;// Audio
SharedPreferences sp;
Context context ;
public Popup_Menu(final Context context) {
this.context= context;
sp = this.context.getSharedPreferences("text", Context.MODE_PRIVATE);
// LinearLayout viewGroup = (LinearLayout) view.findViewById(R.id.popup);
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
layout = layoutInflater.inflate(R.layout.popup_layout_menu, viewGroup);
btn_audio = (Button) layout.findViewById(R.id.btn_audio); // audio
btn_audio.setOnClickListener(this);
}
You can now create a new Popup_Menu(HomeActivity.this)
Yeah...!!! Done. It now working.it doesn't need to create new object. I must change modifire of (object)player and pause() methode and everything in it, to STATIC. So righte now i can access all objects and variables.
Thanks cricket_007...
Related
I'm a new in RxJava in Android development. I had changed in project AsyncTask to RxJava and got a ConcurrentModificationException. Yes, I used collection (sparseArray) but it doesn't matter 'cause exeption was thrown in findViewById.setVisibility. Only when I try to invoke setVisibility. I confused, what I do wrong? I have a TextView in fragment. At first I set up OnClickListener, in listener I init Single.fromCallable, then I set up OnDragListener
TextView tv;
tv.setOnClickListener(v -> {
if (isClickEnable) {
tv.setBackgroundResource(R.drawable.cheap_dark);
cheapInObservable(tv);
}
});
tv.setOnDragListener(new MyDragListener());
private void cheapInObservable(TextView tView) {
Single.fromCallable( () -> tView).subscribeOn(Schedulers.io())
.delay(250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.doOnSuccess(this::onSuccessCheapIn)
.subscribe();
}
And in this code I get exeption:
private class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
View dragView = (View)event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENDED:
if(!event.getResult()) {
if(dragView == v) {
dragView.setVisibility(View.VISIBLE);
activity.findViewById(sparseCheaps.get(dragView.getId())).
setVisibility(View.VISIBLE);
}
}
break;
private void onSuccessCheapIn(TextView tv) {
tv.setVisibility(View.INVISIBLE);
TextView tvd = activity.findViewById(sparseCheaps.get(tv.getId()));
tvd.setVisibility(View.VISIBLE);
AnimatorSet set = new AnimatorSet();
tvd.animate().rotation(0);
int up = activity.findViewById(R.id.guidelineGlowUp).getTop();
int left = getXcoord(tvd);
set.setDuration(400).playTogether(ObjectAnimator.ofFloat(tvd, TextView.TRANSLATION_X,
tvd.getX(), left),
ObjectAnimator.ofFloat(tvd, TextView.TRANSLATION_Y, tvd.getY(), up - 3));
set.setInterpolator(new AccelerateInterpolator((float) 0.4));
set.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
isAnimationCheaps = false;
super.onAnimationEnd(animation);
}
});
set.start();
}
I've found out that only the exeption is thrown when I use setVisibility. If I use AsyncTask instead of Rx it works without exception
StackTrace is:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:795)
at java.util.HashMap$KeyIterator.next(HashMap.java:822)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1154)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:4322)
at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:103)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3407)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Solution:
The following worked fine, but I had two Activities already using my helper class, but forgot to add the Interface to one Activity.
So I got the TypeCastException.
But I did not realize I always looked at the wrong Activity, so I could not find the issue.
I currently write my first android service, which works fine.
Now I realized, that I have to bind unbind, check binding all over and so I thought to put all into a subclass and only do an interface with callbacks for the only functions I need.
But now i am stuck to call my interface functions
onTcBlueServiceConnected()
onTcBlueServiceDisconnected()
I simply want to prevent to write the same code over and over again.
Each activity I have to use the service, I extend from this helper class, instead of the AppCompatActivity.
Maybe I chose the wrong path, but lost with the right words for a search.
As far as I understand bound services, I have to bind from each activity I access the service, and unbind of course when it is destroyed.
So I thought this is a good approach
public abstract class TcBlueServiceHelper extends AppCompatActivity {
private static String LOG_TAG = "TcBlueServiceHelper";
TcBlueService mTcBlueService;
TcBlueService.TcBlueBinder myBinder;
boolean mServiceBound = false;
Context mCallingContext;
public TcBlueServiceHelper(Context callingContext) {
mCallingContext = callingContext;
}
public TcBlueServiceHelper(){
mCallingContext = this;
}
public void bindServiceSave(){
if (mServiceBound == false) {
Intent intent = new Intent(this, TcBlueService.class);
startService(intent);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
}
public void unbindServiceSave(){
if (mServiceBound) {
unbindService(mServiceConnection);
mServiceBound = false;
}
}
public TcBlueService.TcBlueBinder getBinder(){
return myBinder;
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
mServiceBound = false;
((ITcBlueServiceHelper)mCallingContext).onTCBServiceDisconnected(name);
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (TcBlueService.TcBlueBinder) service;
mTcBlueService = myBinder.getService();
mServiceBound = true;
((ITcBlueServiceHelper)mCallingContext).onTCBServiceConnected(name, service);
}
};
public interface ITcBlueServiceHelper{
public void onTCBServiceConnected(ComponentName name, IBinder service);
public void onTCBServiceDisconnected(ComponentName name);
}
}
My first thought to cast result in a "ClassCastException"
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: de.mobacomp.android.freightweight.StartActivity cannot be cast to de.mobacomp.android.freightweight.TcBlueServiceHelper$ITcBlueServiceHelper
at de.mobacomp.android.freightweight.TcBlueServiceHelper$1.onServiceConnected(TcBlueServiceHelper.java:72)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1068)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1085)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
I seem to be fighting with a race condition, the cause of which I can't seem to pin down. When executing the below code, I intermittently get the stack trace below.
Is there some obvious rule of the Fragment lifecycle I am disobeying? I am not clear on what would explicitly forbid me from performing a transaction here to handle the event.
I am using a WebViewClient to detect external URLs clicked within a local .html document - as in, URLs which point to a non-local host. I am using Otto's EventBus to post those actions to an Activity. When the Activity receives those events, I want to show those external URLs in a different Fragment, by calling FragmentTransaction.replace()
DefaultWebViewClient.java
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
boolean shouldOverride;
if (urlIsLocal(url)) {
shouldOverride = super.shouldOverrideUrlLoading(view, url);
} else {
// trigger an event for the fragment to swap out
// return true to tell the webview not to load it...
EventBus.getInstance().post(new LoadExternalUrlEvent(url));
shouldOverride = true;
}
return shouldOverride;
}
FragmentActivity.java
#Subscribe
public void onLoadExternalUrlEvent(LoadExternalUrlEvent externalLoadEvent) {
final BrowserFragment browserFragment = new BrowserFragment();
Bundle args = new Bundle();
args.putSerializable(BrowserFragment.ARG_LOAD_EXTERNAL_URL_EVENT, externalLoadEvent);
browserFragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, browserFragment, BrowserFragment.FRAGMENT_TAG)
.addToBackStack(null).commit();
}
LoadExternalUrlEvent.java
public class LoadExternalUrlEvent implements Serializable {
private static final long serialVersionUID = 1L;
public final String url;
public LoadExternalUrlEvent(String url) {
this.url = url;
}
#Override
public String toString() {
return "LoadExternalUrlEvent [url=" + url + "]";
}
}
EventBus.java
import com.squareup.otto.Bus;
public class EventBus {
private static Bus _INSTANCE;
public static synchronized Bus getInstance() {
if (null == _INSTANCE) {
_INSTANCE = new Bus();
}
return _INSTANCE;
}
}
Stack trace
java.lang.RuntimeException: Could not dispatch event: class <omitted>.LoadExternalUrlEvent to handler [EventHandler public void <omitted>Activity.onLoadExternalUrlEvent(<omitted>LoadExternalUrlEvent)]: Can not perform this action after onSaveInstanceState
at com.squareup.otto.Bus.throwRuntimeException(Bus.java:456)
at com.squareup.otto.Bus.dispatch(Bus.java:386)
at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:367)
at com.squareup.otto.Bus.post(Bus.java:336)
at <omitted>DefaultWebViewClient.shouldOverrideUrlLoading(DefaultWebViewClient.java:51)
at com.android.webview.chromium.WebViewContentsClientAdapter.shouldOverrideUrlLoading(WebViewContentsClientAdapter.java:293)
at com.android.org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:96)
at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at <omitted>Activity.run(<omitted>Activity.java:162)
at <omitted>Activity.onLoadExternalUrlEvent(<omitted>Activity.java:156)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.squareup.otto.EventHandler.handleEvent(EventHandler.java:89)
at com.squareup.otto.Bus.dispatch(Bus.java:384)
... 15 more
A/libc(2689): Fatal signal 6 (SIGABRT) at 0x00000a81 (code=-6), thread 2689
I discovered the problem.
Because I was calling EventBus.register() in Activity.onCreate() I was getting multiple instances of the Activity on my backstack which would act as responders to these events.
The solution is to either register your Activity as late as possible with
#Override
protected void onResume() {
super.onResume();
EventBus.getInstance().register(this);
}
#Override
protected void onPause() {
EventBus.getInstance().unregister(this);
super.onPause();
}
or to declare your Activity as a single instance with
android:launchMode="singleTask"
I have developed simple android imageview that uses viewpager, and it plays music in background. It also stops music when last image is reached and it will resume the music when user slides back to the images. However, my main problem is that when device goes to sleep music stops and when device starts again instead of resuming music again and displaying image..It force closes... Any suggestion on how to fix this issue...Following are my codes...
Mainactivity.java
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
MediaPlayer oursong;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.seekTo(0);
oursong.start();
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
if (pos == adapter.getCount() - 1)
{
oursong.pause();
} else if (!oursong.isPlaying())
{
oursong.start();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
#Override
protected void onPause() {
super.onPause();
oursong.release();
}
}
ImageAdapter.java
import java.io.IOException;
import android.app.WallpaperManager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageAdapter extends PagerAdapter {
Context context;
private final int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(GalImages[position]);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
try {
myWallpaperManager.setResource(GalImages[position]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Logcat errors.... (Following Logcat was taken when app was running on actual device)
08-29 02:48:21.051: I/dalvikvm(2880): Could not find method android.widget.ShareActionProvider.setShareIntent, referenced from method com.manishkpr.viewpagerimagegallery.MainActivity.setShareIntent
08-29 02:48:21.051: W/dalvikvm(2880): VFY: unable to resolve virtual method 3259: Landroid/widget/ShareActionProvider;.setShareIntent (Landroid/content/Intent;)V
08-29 02:48:21.051: D/dalvikvm(2880): VFY: replacing opcode 0x6e at 0x0006
08-29 02:48:21.066: I/dalvikvm(2880): Could not find method android.view.MenuItem.getActionProvider, referenced from method com.manishkpr.viewpagerimagegallery.MainActivity.onCreateOptionsMenu
08-29 02:48:21.066: W/dalvikvm(2880): VFY: unable to resolve interface method 2912: Landroid/view/MenuItem;.getActionProvider ()Landroid/view/ActionProvider;
08-29 02:48:21.066: D/dalvikvm(2880): VFY: replacing opcode 0x72 at 0x0010
08-29 02:48:21.066: D/dalvikvm(2880): VFY: dead code 0x0013-0019 in Lcom/manishkpr/viewpagerimagegallery/MainActivity;.onCreateOptionsMenu (Landroid/view/Menu;)Z
08-29 02:48:21.230: W/MediaPlayer-cpp(2880): info/warning (802, 0)
08-29 02:48:21.348: I/MediaPlayer(2880): Info (802,0)
08-29 02:48:21.434: D/dalvikvm(2880): GC_EXTERNAL_ALLOC freed 1117 objects / 212256 bytes in 71ms
08-29 02:48:36.644: D/dalvikvm(2880): GC_EXTERNAL_ALLOC freed 553 objects / 29584 bytes in 32ms
08-29 02:50:00.566: D/AndroidRuntime(2880): Shutting down VM
08-29 02:50:00.566: W/dalvikvm(2880): threadid=1: thread exiting with uncaught exception (group=0x4001d8a8)
08-29 02:50:00.605: E/AndroidRuntime(2880): FATAL EXCEPTION: main
08-29 02:50:00.605: E/AndroidRuntime(2880): java.lang.RuntimeException: Unable to resume activity {com.manishkpr.viewpagerimagegallery/com.manishkpr.viewpagerimagegallery.MainActivity}: java.lang.IllegalStateException
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2059)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.os.Looper.loop(Looper.java:123)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-29 02:50:00.605: E/AndroidRuntime(2880): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 02:50:00.605: E/AndroidRuntime(2880): at java.lang.reflect.Method.invoke(Method.java:521)
08-29 02:50:00.605: E/AndroidRuntime(2880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-29 02:50:00.605: E/AndroidRuntime(2880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-29 02:50:00.605: E/AndroidRuntime(2880): at dalvik.system.NativeStart.main(Native Method)
08-29 02:50:00.605: E/AndroidRuntime(2880): Caused by: java.lang.IllegalStateException
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.media.MediaPlayer.seekTo(Native Method)
08-29 02:50:00.605: E/AndroidRuntime(2880): at com.manishkpr.viewpagerimagegallery.MainActivity.onResume(MainActivity.java:86)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.Activity.performResume(Activity.java:3823)
08-29 02:50:00.605: E/AndroidRuntime(2880): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
08-29 02:50:00.605: E/AndroidRuntime(2880): ... 10 more
08-29 02:50:10.371: I/Process(2880): Sending signal. PID: 2880 SIG: 9
Have you tried moving your music playback to onResume() Activity lifecycle with fragments won't call onCreate() again until you activity is 're created. So the playback won't be resumed without closing your app.
Something like this
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
MediaPlayer oursong;
ViewPager viewPager;
ImageAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.seekTo(0);
oursong.start();
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(MyViewPagerListener);
}
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
#Override
protected void onPause() {
super.onPause();
if(oursong != null){
oursong.release();
}
}
#Override
protected void onResume(){
super.onResume();
/*
* This is the important part, basically since your releasing the song
* in onPause() you are getting rid of its reference, in this case check
* if your song is null then if it is re-create it, else you can reuse the
* the original, but i suspect that calling release() in onPause() allows the
* song to get cleaned up by Java's Garbage Collector.
*/
if(oursong == null){
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.seekTo(0); // You will probably want to save an int to restore here
oursong.start();
}else{
oursong.seekTo();
oursong.start();
}
}
/*
* May want to add two methods here: onSaveInstanceState(Bundle outstate) &
* onRestoreInstanceState(Bundle savedInstanceState) to maintain playback position
* in onResume instead of just restarting the song.
*/
private final OnPageChangeListener MyViewPagerListener = new OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
if (pos == adapter.getCount() - 1){
// adding null checks for safety
if(oursong != null){
oursong.pause();
}
} else if (!oursong.isPlaying()){
// adding null check for safety
if(oursong != null){
oursong.start();
}
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
};
}
Hope this helps you resolve your problem.
i try to make a app, witch listen also to a touch event, even when it is running in the background, here i found a solution in the forum, but i can't get i running
How can a service listen for touch gestures/events?
if i understood it right, the way to do this, is to set up a new view with the right windowmanager parameters, here is my code!
main activity:
public class MainActivity extends Activity {
View mView;
HUD mHud;
HUDView mHUDView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHud = new HUD();
mHUDView = new HUDView(this);
}
#Override
public boolean onTouchEvent(MotionEvent e) {
mHud.onCreate(this);
return 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.main, menu);
return true;
}
}
at the moment, for testing i just want to change the view, with the first touch,
public class HUD extends Service {
HUDView mView;
public void onCreate(Context mContext) {
super.onCreate();
mView = new HUDView(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
class HUDView extends ViewGroup {
public HUDView(Context context) {
super(context);
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("ontouch", "clicked in the HUD View");
//Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show();
return true;
}
}
I'am not very confident in java, the error i get is a javaNullPointerException Error. I know this happend because a object became null, but how do i pass the object to the class and to the subclass.
here the log:
11-18 11:56:03.791: E/AndroidRuntime(5095): FATAL EXCEPTION: main
11-18 11:56:03.791: E/AndroidRuntime(5095): java.lang.NullPointerException
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.view.View.<init>(View.java:1810)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.view.ViewGroup.<init>(ViewGroup.java:288)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.holzi.runinbackground.HUDView.<init>(HUD.java:47)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.holzi.runinbackground.HUD.onCreate(HUD.java:22)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.holzi.runinbackground.MainActivity.onTouchEvent(MainActivity.java:32)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.os.Looper.loop(Looper.java:143)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.app.ActivityThread.main(ActivityThread.java:4914)
11-18 11:56:03.791: E/AndroidRuntime(5095): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 11:56:03.791: E/AndroidRuntime(5095): at java.lang.reflect.Method.invoke(Method.java:521)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
thx!
You're passing this as the context to HUDView, this is your Service, which does not have a base context until it is attached, and thus will throw if getResources() is called.
You'll need to wait until attachBaseContext is called, or use another Context to instantiate HUDView instead of the service, perhaps the Application context.
Here the complete working code to just log any touch on the mobile, catched by the invisible system alert overlay
public class InvisibleView extends Service {
View myView;
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(Context myContext) {
super.onCreate();
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = new View(myContext);
myView = inflater.inflate(R.layout.invisibleviewxml, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) myContext.getSystemService(WINDOW_SERVICE);
wm.addView(myView, params);
myView.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch(View inviView, MotionEvent event) {
Log.d("tag", "touch caught by invisble running service");
return true;
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
if(myView != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(myView);
myView = null;
}
}
}
Main Activity:
public class MainActivity extends Activity {
InvisibleView myInvisibleView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myInvisibleView = new InvisibleView();
loadInvisibleView();
sendScreenHome();
}
private void loadInvisibleView(){
myInvisibleView.onCreate(this.getApplicationContext());
}
private void sendScreenHome(){
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
}
}
invisibleviewxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="programm is running ..."
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>