我创造了一个包含多个片段的活动。当在片段内部单击按钮(图像)时,片段将被替换为新片段。这很好用。 其中一个图像/按钮是打开添加页面的“添加”按钮。在这个片段中有几个EditTexts和一个按钮'Insert'。
按“插入”按钮时,必须检索Edittext值。这是它出错的地方。
这是我的代码(我排除了似乎没有必要的代码): MainActicity.java:
public class MainActivity extends AppCompatActivity {
FragmentTransaction fragmentTransaction;
EditText etDistancesInsertDays;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etDistancesInsertDays = (EditText)findViewById(R.id.etDistancesInsertDays);
// On create display home page (HomeFragment)
Fragment frRecords = new RecordsFragment();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, frRecords);
fragmentTransaction.commit();
}
public void openDistancesInsert(){
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new DistanceInsertFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle(getString(R.string.frTitleDistanceInsert));
}
public void switchFragment(View view){
switch (view.getId()) {
case R.id.btnInsertDistance:
// Trial 1
EditText etText = (EditText)findViewById(R.id.etDistancesInsertDays);
String strDays = etDistancesInsertDays.getText().toString();
// Caused by: java.lang.NullPointerException
Toast.makeText(MainActivity.this, "Days: "+strDays, Toast.LENGTH_LONG).show();
// Trial 2
Fragment frInsert;
frInsert = getSupportFragmentManager().findFragmentById(R.id.frDistanceInsert);
View frViewInsert;
frViewInsert = frInsert.getView();
// Caused by: java.lang.NullPointerException
EditText etText = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertDays);
String strDays = etDistancesInsertDays.getText().toString();
Toast.makeText(MainActivity.this, "Days: "+strDays, Toast.LENGTH_LONG).show();
break;
}
}
}
我的DistanceInsertFragment.java
public class DistanceInsertFragment extends Fragment {
EditText etDistancesInsertDays, etDistancesInsertMonths, etDistancesInsertYears;
public DistanceInsertFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View frViewInsert = inflater.inflate(R.layout.fragment_distance_insert, container, false);
// Set current dat in text field:
Calendar c = Calendar.getInstance();
String strDayOfMonth = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
String strMonthOfYear = Integer.toString(c.get(Calendar.MONTH)+1);
String strYear = Integer.toString(c.get(Calendar.YEAR));
etDistancesInsertDays = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertDays);
etDistancesInsertMonths = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertMonths);
etDistancesInsertYears = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertYears);
etDistancesInsertDays.setText(strDayOfMonth);
etDistancesInsertMonths.setText(strMonthOfYear);
etDistancesInsertYears.setText(strYear);
return frViewInsert;
}
}
试验1和试验2都给出了错误“引起:java.lang.NullPointerException”。虽然我不确定用它是什么意思..
答案 0 :(得分:0)
代码不应该是:
EditText etText = (EditText)findViewById(R.id.etDistancesInsertDays);
String strDays = etText.getText().toString();
在这里,您将从EditText获取文本,在您尝试从id etDistancesInsertDays中检索的代码中