0

I'm using this project https://github.com/googlesamples/android-RecyclerView as reference to learn about RecyclerView, and implementing the answer by MLProgrammer-Cim here to handle onClick events.

In my CustomAdapteractivity I'm trying to start a method speakNow(); which is in another activity MainActivity

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
...

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ...
    public MyViewHolderClicks mListener;
    private Context context;
    MainActivity ma = new MainActivity();

//        FragmentTransaction ft =     ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
//        SampleActivityBase fragment = (SampleActivityBase)ft.findFragmentByTag("SampleActivityBase");


    public ViewHolder(View myv, MyViewHolderClicks listener) {

        super(myv);
        mListener = listener;
        ....
        imageIcon.setOnClickListener(this);
        myv.setOnClickListener(this);
        this.context =context;


    }
    @Override
    public void onClick(View v) {
       int position = getLayoutPosition();
        if (v instanceof Button){
            mListener.onTomato((Button) v, getLayoutPosition());
            switch (v.getId()) {

               case R.id.button_for_cardview:
                   ma.speakNow();
 .....
 }
}

My MainActivity

public class MainActivity extends SampleActivityBase {
....
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     ....
    //Intent intent = new Intent(getApplicationContext(),     CustomAdapter.class);
    //startActivityForResult(new Intent(this, CustomAdapter.class), checka);
    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        RecyclerViewFragment fragment = new RecyclerViewFragment();
        transaction.replace(R.id.sample_content_fragment, fragment);
        transaction.commit();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == checka && resultCode == RESULT_OK){
        ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));

       .....
        }

    }
    super.onActivityResult(requestCode, resultCode, data);


}

protected void speakNow() {
    Intent a = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    a.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    a.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
    a.putExtra(RecognizerIntent.EXTRA_PROMPT, GlobalVariables.getInstance().getRightAnswer1());
    startActivityForResult(a, checka);
}
....

}

This return a NullPointerException in MainActivity.speakNow(MainActivity.java:478) which is this startActivityForResult(a, checka); and Adapter$ViewHolder.onClick(CustomAdapter.java:287) which is ma.speakNow();

I've read various "calling Methods from other activities returning NPE", questions and answers,not too sure how those apply to my case since MainActivity extends SampleActivityBase which is a public abstract class which extends FragmentActivity.

Where / how do I need to intialize the MainActivity ma = new MainActivity(); so its not null? Thanks

Community
  • 1
  • 1
Mcorv
  • 161
  • 3
  • 12

1 Answers1

0

the exception is thrown in startActivityForResult, that is because the Activity you create by MainActivity ma = new MainActivity(); is different from the Activity create by system, the Activity you created has nothing in it, read the source code of Activity to execute startActivityForResult, it depends on some member variable.so you got the nullpoint exception. you can add a setter in you adapter, pass the reference of Activity to adapter

public void setActivity(Activity aty) {
    this.aty = aty;
}
rainash
  • 864
  • 6
  • 15
  • Thanks for the reply. Im sure that is a very complete answer, but due to my limitations I don't understand how to pass the reference of activity to adapter and how to set it to fire the speakNow() method..could you elaborate using the code I posted above..thanks – Mcorv Jul 11 '15 at 17:28