2016-08-04 105 views
0

我創建了一個具有多個片段的活動。當在一個片段中點擊一個按鈕(圖片)時,片段被替換爲一個新的片段。這工作正常。 其中一個圖像/按鈕是打開添加頁面的「添加」按鈕。在這個片段中有幾個EditText和一個按鈕'Insert'。通過onClick從片段獲取EditText值

當按下「插入」按鈕時,必須檢索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

其中是您的按鈕在activity_main佈局或fragment_distance_insert佈局?因爲我沒有看到任何onclicklisterner定義。 –

+0

我看到你的Activity中有一個onClick方法,該按鈕是否假設在一個片段內而另一個片段內的EditText? –

+0

onClick確實在Activity內。 Button和EditText在同一個Fragment中。我的錯誤已經被亞倫發現了,我將這些錯誤分配給了他。 – Chris

回答

0

應的代碼不會:

EditText etText = (EditText)findViewById(R.id.etDistancesInsertDays); 
String strDays = etText.getText().toString(); 

在這裏,您從EditText上獲取文本,在你的代碼試圖從ID檢索etDistancesInsertDays

+0

我的天啊。我在這裏失敗了。感謝您的發現!我應該刪除這個問題,因爲這只是一個閱讀錯誤。 – Chris

+0

沒問題快樂編碼:) – Aaron