Reputation: 114
I just wanted to get the value of the edit-text in the fragment and display it in textView by creating a String function and return the text->String and a another String function to return the value from the edittext in fragment and display it with Button Save(just for testing) in my MainActivity, But it keep giving me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.Thisporject.fragment.fragment_fr_Person.TogetNameText(fragment_fr_Person.java:97)
at com.example.Thisporject.CreateGame.CreateGameGenerated.getNameEditText(CreateGameGenerated.java:60)
at com.example.Thisporject.CreateGame.CreateGameGenerated.SaveAllContent(CreateGameGenerated.java:79)
FragmentActivity.Java:
public class fragment_fr_Person extends Fragment {
View rootView;
EditText NameEdit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_fr_person, container, false);
NameEdit = rootView.findViewById(R.id.Name_Editext);
return rootView;
}
public String TogetNameText(){
return NameEdit.getText().toString();
}
}
MainActivity.Java:
public class CreateGameGenerated extends FragmentActivity {
Button Save_btn;
ImageView displayImage;
FloatingActionButton addPhotoFloating_btn;
fragment_list frag;
List<fragment_list> qqf = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_game_generated);
Save_btn = findViewById(R.id.Save_btn);
displayImage = findViewById(R.id.display_ImageView);
addPhotoFloating_btn = findViewById(R.id.photo_gallery_acess_btn);
TextView testName = findViewById(R.id.testName);
TextView testAge = findViewById(R.id.testAge);
Spinner spinner = findViewById(R.id.tags_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tags, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Save_btn.setOnClickListener(this::SaveAllContent);
}
public String getNameEditText(){
fragment_fr_Person fragment_fr_person;
fragment_fr_person = new fragment_fr_Person();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.disallowAddToBackStack();
fragmentTransaction.commitNowAllowingStateLoss();
return fragment_fr_person.TogetNameText();
}
public String getAgeEditText(){
fragment_fr_Person fragment_fr_person;
fragment_fr_person = new fragment_fr_Person();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.disallowAddToBackStack();
fragmentTransaction.commitNowAllowingStateLoss();
//Handler
return fragment_fr_person.TogetAgeText();
}
private void SaveAllContent(View view) {
TextView testName = findViewById(R.id.testName);
TextView testAge = findViewById(R.id.testAge);
testName.setText(getNameEditText());
testAge.setText(getAgeEditText());
}
}
fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/EditTextMainContainer"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@id/ImageViewAndEdit"
app:layout_constraintStart_toStartOf="parent"
tools:context="com.example.ThisProject.fragment.fragment_fr_Person">
<RelativeLayout
android:layout_marginTop="10dp"
android:id = "@+id/NameContainer"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/textViewName"
android:text="@string/TextName"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginStart="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/Name_Editext"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:autofillHints="@string/template"
android:layout_below="@id/textViewName"
android:background="@drawable/custom_input"
android:hint="@string/insert_here" />
</RelativeLayout>
</RelativeLayout>
MainActivy.xml
<androidx.fragment.app.FragmentContainerView
android:id ="@+id/fragment_generated_mainView"
android:name="com.example.ThisProject.fragment.fragment_fr_Person"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tags_spinner"
/>
<TextView android:id ="@+id/testName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ImageViewAndEdit"/>
Upvotes: 0
Views: 56
Reputation: 399
getNameEditText
this method looks wrong to me
you should get your fragment using findFragmentById()
or save to a variable when you instantiate it. Then you can call your mFragment.TogetNameText()
from your activity
Upvotes: 1
Reputation: 106
You should move the code to get value from EditText to onViewCreated. This will execute the code after the view is created. Also check if R.id.Name_Editext exists in the layout R.layout.fragment_fr_person.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NameEdit = (EditText) rootView.findViewById(R.id.Name_Editext);
}
Upvotes: 0