Reputation: 649
I have a fragment that asks for email and password. I want to transfer the email to the activity.
I want to pass from the fragment the text from etCbMail
, and then return to activity. I keep getting null object and i dont know how to return to the activity.
This is my fragment:
public class RegFragOne extends Fragment {
EditText etCbMail, etCbPassword, etCbVerifyPassword;
Button btnCbContinueToTwo;
OnDataPass dataPasser;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reg_one, container, false);
etCbMail = (EditText) view.findViewById(R.id.etEMail);
etCbPassword = (EditText) view.findViewById(R.id.etPassword);
etCbVerifyPassword = (EditText) view.findViewById(R.id.etVerifyPassword);
btnCbContinueToTwo = (Button) view.findViewById(R.id.btnContinueToTwo);
btnCbContinueToTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataPasser.onDataPass(etCbMail.getText().toString());
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
dataPasser = (OnDataPass) context;
} catch (Exception ex) {
ex.printStackTrace();
}
}
public interface OnDataPass {
public void onDataPass(String data);
}
}
this is my activity:
public class RegistationActivity extends Activity implements RegFragOne.OnDataPass{
Fragment fragment;
FragmentTransaction fragmentTransaction;
String passedData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg_fragment);
fragment = new RegFragOne();
fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.frgContainer, fragment);
fragmentTransaction.commit();
}
@Override
public void onDataPass(String data) {
passedData = data;
}
}
and this is my log:
03-05 15:15:32.871 4043-4043/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shaka.myparselogin, PID: 4043
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.shaka.myparselogin.RegistrationFragments.RegFragOne$OnDataPass.onDataPass(java.lang.String)' on a null object reference
at com.example.shaka.myparselogin.RegistrationFragments.RegFragOne$1.onClick(RegFragOne.java:39)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
can you please help me with my 2 questions?
Upvotes: 1
Views: 296
Reputation: 6419
Replace getFragmentManager()...
with getSupportFragmentManager()
.
This will force you to use the components from the support library ( Fragment
, AppCompatActivity
) and change your imports from import android.app.Fragment;
to import android.support.v4.app.Fragment;
.
As a general rule, try and use the components from the support libraries, not the ones from the framework, as the support components get bugs fixed faster. I have no proof to back this statement, only my day-to-day experience :).
Upvotes: 2