Java 片段NullPointException
我只是想在片段中获取编辑文本的值,并通过创建一个String函数在textView中显示它,然后返回text->String和另一个String函数,以从片段中的edittext返回值,并在我的MainActivity中使用按钮Save(仅用于测试)显示它,但它一直给我这个错误:Java 片段NullPointException,java,android,android-fragments,Java,Android,Android Fragments,我只是想在片段中获取编辑文本的值,并通过创建一个String函数在textView中显示它,然后返回text->String和另一个String函数,以从片段中的edittext返回值,并在我的MainActivity中使用按钮Save(仅用于测试)显示它,但它一直给我这个错误: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditT
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();
}
}
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());
}
}
MainActivity.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();
}
}
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());
}
}
public类CreateGameGenerated扩展碎片活动{
按钮保存按钮;
图像视图显示图像;
浮动操作按钮添加光浮动\u btn;
碎片列表碎片;
List qqf=new ArrayList();
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.create_game_generated);
Save_btn=findviewbyd(R.id.Save_btn);
displayImage=findviewbyd(R.id.display\u ImageView);
addPhotoFloating\u btn=findviewbyd(R.id.photo\u gallery\u access\u btn);
TextView testName=findviewbyd(R.id.testName);
TextView testAge=findviewbyd(R.id.testAge);
微调器微调器=findViewById(R.id.tags\u微调器);
ArrayAdapter=ArrayAdapter.createFromResource(这个,R.array.tags,android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
Save_btn.setOnClickListener(this::SaveAllContent);
}
公共字符串getNameEditText(){
碎片人碎片人;
fragment_fr_person=新的fragment_fr_person();
FragmentTransaction FragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.disallowAddToBackStack();
fragmentTransaction.commitNowAllowingStateLoss();
返回片段_fr_person.TogetNameText();
}
公共字符串getAgeEditText(){
碎片人碎片人;
fragment_fr_person=新的fragment_fr_person();
FragmentTransaction FragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.disallowAddToBackStack();
fragmentTransaction.commitNowAllowingStateLoss();
//处理者
返回fragment_fr_person.togetext();
}
私有void SaveAllContent(视图){
TextView testName=findviewbyd(R.id.testName);
TextView testAge=findviewbyd(R.id.testAge);
setText(getNameEditText());
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>
maintactivy.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"/>
您应该将代码从EditText获取值移动到onViewCreated。这将在创建视图后执行代码。还要检查布局中是否存在R.id.Name\u Editext R.layout.fragment\u fr\u person
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NameEdit = (EditText) rootView.findViewById(R.id.Name_Editext);
}
getNameEditText
我觉得这个方法不对
您应该使用
findFragmentById()
获取片段,或者在实例化时将其保存到变量中。然后你可以从你的活动中调用你的mFragment.TogetNameText()
,R.id.NameEdit
确实存在,我把它放在了onViewCreated中,但是到目前为止没有任何效果。在findViewById调用之前添加(EditText)cast。