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 CustomAdapter
activity 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