calling method in mainactivity from another class in android

Lama Tatwany

I have a list view with text view and three image buttons. I'm trying to call a method from the main page when one of the image buttons is clicked. But I got this error:

  04-19 13:02:48.021 30996-30996/com.example.lama.audiotest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.lama.audiotest, PID: 30996
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                            at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
                                                                            at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
                                                                            at android.media.MediaPlayer.create(MediaPlayer.java:1007)
                                                                            at android.media.MediaPlayer.create(MediaPlayer.java:990)
                                                                            at com.example.lama.audiotest.Main3Activity.play(Main3Activity.java:35)
                                                                            at com.example.lama.audiotest.CustomAdapter$1.onClick(CustomAdapter.java:83)
                                                                            at android.view.View.performClick(View.java:5697)
                                                                            at android.view.View$PerformClick.run(View.java:22526)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:158)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

I tried moving this line:

mp= MediaPlayer.create(Main3Activity.this,R.raw.alfatiha);

to the onCreate(); and then it worked, but I want to play different audio depending on the string I got with the play method. how to solve this?

my mainactivity:

public class Main3Activity extends Activity {

private static final String TAG = Main3Activity.class.getSimpleName();
static MediaPlayer mp;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    ListView listView = (ListView) findViewById(R.id.list_data);

    CustomAdapter customAdapter = new CustomAdapter();

    listView.setAdapter(customAdapter);

}

public void play(String name){
    if (name.equals("الفاتحه")){
        mp= MediaPlayer.create(Main3Activity.this,R.raw.alfatiha);
        mp.start();
    }
}

public void pause(){
        mp.pause();
}

public void stop(){
    mp.stop();
}
}

my list adapter:

public class CustomAdapter extends BaseAdapter {
Main3Activity  main= new Main3Activity();
private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;

public CustomAdapter() {
    listArray = new ArrayList<DataModel>(5);
    listArray.add(new DataModel("الفاتحه", 5, 1.8, "Java"));
    listArray.add(new DataModel("البقرة", 10, 2.8, "Python"));
    listArray.add(new DataModel("ال عمران", 15, 3.8, "Django"));

}

@Override
public int getCount() {
    return listArray.size();    // total number of elements in the list
}

@Override
public Object getItem(int i) {
    return listArray.get(i);    // single item in the list
}

@Override
public long getItemId(int i) {
    return i;                   // index number
}

@Override
public View getView(int index, View view, final ViewGroup parent) {

    if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.list, parent, false);
    }

    final MediaPlayer mp;


    final DataModel dataModel = listArray.get(index);

    TextView textView = (TextView) view.findViewById(R.id.Itemname);
    textView.setText(dataModel.getName());

    ImageButton play = (ImageButton) view.findViewById(R.id.btnplay);
    ImageButton pause = (ImageButton) view.findViewById(R.id.btnpause);
    ImageButton stop = (ImageButton) view.findViewById(R.id.btnstop);

    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            main.play(dataModel.getName());
           // Toast.makeText(parent.getContext(), "you clicked play for: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
        }
    });

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

            main.pause();

          //  Toast.makeText(parent.getContext(), "you clicked pause for: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
        }
    });
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            main.stop();
          //  Toast.makeText(parent.getContext(), "you clicked stop for: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
        }
    });



    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "string: " + dataModel.getName());
            Log.d(TAG, "int: " + dataModel.getAnInt());
            Log.d(TAG, "double: " + dataModel.getaDouble());
            Log.d(TAG, "otherData: " + dataModel.getOtherData());

            Toast.makeText(parent.getContext(), "view clicked: " + dataModel.getOtherData(), Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}
}   
AlexTa

In your CustomAdapter you are creating a new instance of Main3Activity instead of passing your current Main3Activity instance as an argument for your Custom adapter. Because of that, your are getting a null pointer exeception when you are trying to access to Main3Activity methods.

To solve your problem modify your CustomAdapter constructor to allow Main3Activity as argument:

public class CustomAdapter extends BaseAdapter {
    Main3Activity  main;
    private static final String TAG = CustomAdapter.class.getSimpleName();
    ArrayList<DataModel> listArray;

    public CustomAdapter(Main3Activity main) {
        this.main = main;
        listArray = new ArrayList<DataModel>(5);
        listArray.add(new DataModel("الفاتحه", 5, 1.8, "Java"));
        listArray.add(new DataModel("البقرة", 10, 2.8, "Python"));
        listArray.add(new DataModel("ال عمران", 15, 3.8, "Django"));
    }

Then, back into your Main3Activity.class, pass activity instance to CustomAdapter:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    ListView listView = (ListView) findViewById(R.id.list_data);
    CustomAdapter customAdapter = new CustomAdapter(this);
    listView.setAdapter(customAdapter);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a method of the MainActivity from another class?

From Dev

Calling a method of the MainActivity from another class?

From Dev

Android - Calling a method from MainActivity into Fragment

From Dev

Calling a class from another class with main method

From Dev

Calling a method from one class in another class

From Dev

Calling a method from inside of another class

From Dev

Calling static method from another java class

From Dev

Calling the paint() method from another class?

From Dev

Trouble calling method from another class

From Dev

Trouble calling a method from another class

From Dev

Calling instance method from another class ruby

From Dev

calling a boolean method from another class

From Dev

Calling a method from another class that removes views

From Dev

Calling a method from another class in broadcast receiver

From Dev

Calling method from another class unsuccessful

From Dev

Calling StartActivity(intent) method from another class

From Dev

Calling a method from inside of another class

From Dev

Python: Calling a decorator method from another class

From Dev

calling a boolean method from another class

From Dev

Having trouble calling a method from another class

From Dev

JGroups RpcDispatcher calling method from another class

From Dev

Calling a class method from another class method in Python 2.7

From Dev

Calling a class method from another class method in Python 2.7

From Dev

Calling publishProgress from another method in Android

From Dev

Android studios how to pass a context from mainactivity to another class

From Dev

calling a button or a textfield from another class | android

From Dev

Calling a function from another class? Android

From Dev

Typescript: Calling method from another method of current class

From Dev

Calling an object and its method from another object class method

Related Related

  1. 1

    Calling a method of the MainActivity from another class?

  2. 2

    Calling a method of the MainActivity from another class?

  3. 3

    Android - Calling a method from MainActivity into Fragment

  4. 4

    Calling a class from another class with main method

  5. 5

    Calling a method from one class in another class

  6. 6

    Calling a method from inside of another class

  7. 7

    Calling static method from another java class

  8. 8

    Calling the paint() method from another class?

  9. 9

    Trouble calling method from another class

  10. 10

    Trouble calling a method from another class

  11. 11

    Calling instance method from another class ruby

  12. 12

    calling a boolean method from another class

  13. 13

    Calling a method from another class that removes views

  14. 14

    Calling a method from another class in broadcast receiver

  15. 15

    Calling method from another class unsuccessful

  16. 16

    Calling StartActivity(intent) method from another class

  17. 17

    Calling a method from inside of another class

  18. 18

    Python: Calling a decorator method from another class

  19. 19

    calling a boolean method from another class

  20. 20

    Having trouble calling a method from another class

  21. 21

    JGroups RpcDispatcher calling method from another class

  22. 22

    Calling a class method from another class method in Python 2.7

  23. 23

    Calling a class method from another class method in Python 2.7

  24. 24

    Calling publishProgress from another method in Android

  25. 25

    Android studios how to pass a context from mainactivity to another class

  26. 26

    calling a button or a textfield from another class | android

  27. 27

    Calling a function from another class? Android

  28. 28

    Typescript: Calling method from another method of current class

  29. 29

    Calling an object and its method from another object class method

HotTag

Archive