rajagrawal
rajagrawal

Reputation: 631

MediaPlayer and Fragments - java.lang.IllegalStateException: Could not execute method of the activity

I have set up a MainActivity with multiple tabs that can be navigated by swipes. So, whenever I swipe to the respective tab, a fragment would display the imageview described in it's XML, and this imageview can enable audio playback when tapped.

The imageview is setup inside frag.xml with the respective android:onClick attribute.

So for simplicity, let's consider 1 fragment (hooked to frag.xml) and 1 main activity (hooked to main.xml).

Now, whenever I tap on the imageview the app doesn't play any sound, but immediately crashes with the following logcat report:

01-19 15:36:29.211: E/AndroidRuntime(20718): FATAL EXCEPTION: main
01-19 15:36:29.211: E/AndroidRuntime(20718): java.lang.IllegalStateException: Could not execute method of the activity
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.view.View$1.onClick(View.java:3640)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.view.View.performClick(View.java:4247)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.view.View$PerformClick.run(View.java:17728)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.os.Handler.handleCallback(Handler.java:730)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.os.Looper.loop(Looper.java:137)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.app.ActivityThread.main(ActivityThread.java:5289)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at java.lang.reflect.Method.invoke(Method.java:525)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at dalvik.system.NativeStart.main(Native Method)
01-19 15:36:29.211: E/AndroidRuntime(20718): Caused by: java.lang.reflect.InvocationTargetException
01-19 15:36:29.211: E/AndroidRuntime(20718):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at java.lang.reflect.Method.invoke(Method.java:525)
01-19 15:36:29.211: E/AndroidRuntime(20718):    at android.view.View$1.onClick(View.java:3635)
01-19 15:36:29.211: E/AndroidRuntime(20718):    ... 11 more
01-19 15:36:29.211: E/AndroidRuntime(20718): Caused by: java.lang.NullPointerException
01-19 15:36:29.211: E/AndroidRuntime(20718):    at com.example.MainActivity.hellofunction(MainActivity.java:63)
01-19 15:36:29.211: E/AndroidRuntime(20718):    ... 14 more
01-19 15:36:30.522: I/Process(20718): Sending signal. PID: 20718 SIG: 9

frag.xml -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollyClips"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="403dp"
        android:layout_marginLeft="1dp"
        android:background="@color/black"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="right"
            android:src="@drawable/ico" />

        <ImageView
            android:id="@+id/ImageView01"
            android:contentDescription="solhello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ico" 
            android:onClick="hellofunction"/>

    </RelativeLayout>

</ScrollView>

My Java codes are as follows -

MainActivity.java :

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

    public class MainActivity extends FragmentActivity implements ActionBar.TabListener
    {   
                private ViewPager viewPager;
                private TabsPagerAdapter adapter;
                private ActionBar actionbar;

                private String[] tabs={"A"};

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

                    //Initialize
                    viewPager=(ViewPager)findViewById(R.id.pager);
                    actionbar=getActionBar();
                    adapter=new TabsPagerAdapter(getSupportFragmentManager());

                    viewPager.setAdapter(adapter);
                    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

                    //Add tabs
                    for(String tab_name:tabs)
                    {
                        actionbar.addTab(actionbar.newTab().setText(tab_name)
                        .setTabListener(this));
                    }
                }

                @Override
                public void onTabReselected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onTabSelected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub
                }

    public void hellofunction(View v)
    {   mp.release();
        mp=MediaPlayer.create(this, R.raw.helloworld);
        mp.start();
    }

    }

ClipsFragment.java :

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ClipsFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.frag, container, false);

        return rootView;

    }   
}

What am I doing wrong? How can fix this and get the audio playback working?

Upvotes: 0

Views: 2914

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You are calling release even before mp is initialized

 mp.release();
 mp=MediaPlayer.create(this, R.raw.helloworld);

Edit:

In fragment

 MediaPlayer mp; 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.frag, container, false);
    ImageView iv = (ImageView) rootView.findViewById(R.id.ImageView01);
    iv.setOnClickListener(new OnClickListener()
    {
             @Override
             public void onClick(View v)
             {
                     mp=MediaPlayer.create(getActivity(), R.raw.helloworld); 
                     mp.start():  
             } 
    });
    return rootView;

}  

and you can remove android:onClick="hellofunction"

Upvotes: 2

Related Questions