私は複数のフラグメントで活性を作りました。フラグメント内でボタン(イメージ)をクリックすると、そのフラグメントが新しいフラグメントに置き換えられます。これは正常に動作します。 これらの画像/ボタンの1つは、追加ページを開く '追加'ボタンです。この断片には、いくつかの編集テキストと「挿入」ボタンがあります。フラグメントからonClickまで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"。私はワットがそれを意味するかわからないけれども...
activity_mainレイアウトまたはfragment_distance_insertレイアウトのボタンはどこですか?私はonclicklisternerは定義されて表示されません。 –
私はあなたのアクティビティの中にonClickメソッドがあるのを見ます、そのボタンは別のフラグメントの中の1つのフラグメントとEditTextの中にあると思いますか? –
onClickは実際にアクティビティ内にあります。 ButtonとEditTextは同じフラグメント内にあります。私の間違いはすでにアーロンによって発見された、私はstrDaysを間違って割り当てた。 – Chris