I'm trying to pass a value from a fragment to an activity and I can't because I get an exception. On my fragment I have to choose one of the horoscopes which are presented in a FrameLayout. To do the communication between Activity and Fragment I'm using callback.
The exception is:
java.lang.NullPointerException: Attempt to invoke interface method
'void
com.converter.android.dailyhoroscope.HoroscopeChoice$OnInfoChangedListener.onInfoChanged(java.lang.String)'
on a null object reference
at
com.converter.android.dailyhoroscope.HoroscopeChoice$1.onClick(HoroscopeChoice.java:133)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
On the Fragment, I have the following code:
public class HoroscopeChoice extends DialogFragment {
public HoroscopeChoice() {
}
/******************************
* Callback
********/
public static void setOnInfoChangedListener(OnInfoChangedListener callback) {
mCallback = callback;
}
public interface OnInfoChangedListener {
public void onInfoChanged(String horosocopo);
}
(...)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_horoscope_choice,
container, false);
Button aquarius;
aquarius = (Button) view.findViewById(R.id.aquarius1);
final int id = view.getId();
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String horoscopo = onClick2(v.getId());
mCallback.onInfoChanged(horoscopo);
}
};
aquarius.setOnClickListener(onClickListener);
return view;
}
public String onClick2(int id)
{
String horoscopo="";
if (id == R.id.aquarius1) {
horoscopo = "Aquarius";
}
(...)
return horoscope;
}
On the activity, if I try to put setOnInfoChangedListener(this);
trying to solve the exception I have, I get another error "Cannot resolve method setOnInfoChangedListener".
Activity:
public class SchedulerActivity extends Activity implements HoroscopeChoice.OnInfoChangedListener {
(...)
setOnInfoChangedListener(this);
public void onInfoChanged(String horoscopo) {
String sign="";
mHoroscopeDisplay = (TextView) findViewById(R.id.dailyHoroscope4);
mHoroscopeDisplay.setText(horoscopo);
saveData(dHosocope, sign);
}
}
Can you help me please?