2014-01-11 5 views
2

몇 가지 문제가 있습니다. 그것은 내가 무작위로 findViewById()에서 반환되는 것 같습니다. TextViewLinearLayout .. 때로는 그냥 null 예외를 제공합니다.FindViewByID 조각 Null

- 이것이 올바르게 반환됩니다. enter image description here - 디버깅 한 경우 결국 이런 식으로 끝날 것입니다 ...

디버깅 중 다른 시간 동안 null을 반환합니다.

어떤 방법 으로든 지속적인 결과를 얻을 수 있습니까?

사례 Java를 기록하십시오.

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_log_acase); 

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 
     mViewPager 
       .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 
         actionBar.setSelectedNavigationItem(position); 
        } 
       }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(actionBar.newTab() 
        .setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.new_case, menu); 
     return true; 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in 
     // the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      if (position == 0) 
      { 
      Fragment fragment = new PatientInfoFragment(); 
      Bundle args = new Bundle(); 
      fragment.setArguments(args); 
      return fragment; 
      } 
      else if (position == 1) 
      { 
       Fragment fragment2 = new EncounterFragment(); 
       Bundle args = new Bundle(); 
       fragment2.setArguments(args); 
       return fragment2; 
      } 
      else if (position == 2) 
      { 
       Fragment fragment3 = new ProcedureFragment(); 
       Bundle args = new Bundle(); 
       fragment3.setArguments(args); 
       return fragment3; 
      } 
      return null; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return "Patient Info"; 
      case 1: 
       return "Encounter"; 
      case 2: 
       return "Procedure"; 
      } 
      return null; 
     } 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     case R.id.ProgressBtn: 
      openProgress(); 
      break; 
     case R.id.SavedCasesBtn: 
      openSaveCases(); 
      break; 
     case R.id.action_newcase_save: 
      openSave(); 
      return true; 
     case R.id.action_newcase_send: 
      openSend(); 
      return true; 
     case R.id.action_settings: 
      Intent intent = new Intent(LogACase.this, PrefsActivity.class); 
      startActivity(intent); 
      break; 
     } 

     return super.onOptionsItemSelected(item); 

    } 

    public void openSave() 
    { 

    } 

    public void openSend() 
    { 
     String encounterValue, procedureValue; 

    encounterValue = retrieveEncounterValue(); 
    procedureValue = retrieveProcedureValue(); 


    } 

    public String retrieveEncounterValue() 
    { 
     String encounterValue = null; 
     //Encounter 
     LinearLayout ListV =(LinearLayout) findViewById(R.id.encounterslist); 
     //Get the number of rows - Correct number of row. 
      for(int i = 0 ; i<ListV.getChildCount();i++) 
      { 
       //get that row 's listV 
       LinearLayout listParent = (LinearLayout) ListV.getChildAt(i); 
       //Nested LL is 2nd item of listParent. 
       LinearLayout nestedLL = (LinearLayout)listParent.getChildAt(1); 
       //Get the checkbox in that nested. 
       CheckBox cb = (CheckBox) nestedLL.getChildAt(0); 

       //If it's checked 
       if(cb.isChecked()) 
       { 
        //take the ID value of the checked and add it to encouterSelected array. 
        TextView idTV = (TextView) listParent.getChildAt(0); 
        encounterSelected.add(idTV.getText()); 
       } 
      } 
      //Sort the array on order before sending it back to NewCases 
      Collections.sort(encounterSelected); 
     StringBuilder full = new StringBuilder(); 
      int B = encounterSelected.size(); 
      if (B != 0) 
      { 
     encounterValue = full.append(encounterSelected).toString(); 
     encounterValue = encounterValue.replaceAll("\\s",""); 
      } 
      else 
      { 
       encounterValue = "No Encounter Selected"; 
      } 


      //Patient Info 


      history = (CheckBox) findViewById(R.id.checkbox_history); 
      physical = (CheckBox) findViewById(R.id.checkbox_physical); 
      patientInitial = (EditText) findViewById(R.id.InitialTextEdit); 
      locSpinner = (Spinner) findViewById(R.id.locationSpinner); 
      dateOfDiagnosis = (EditText) findViewById(R.id.showMyDate); 
      siteLocation = locSpinner.getSelectedItem().toString(); 
      wardClinic = (EditText) findViewById(R.id.WardClinicTextEdit); 
      // Declaration into String 
      String patientInitialText = patientInitial.getText().toString(); 
      String dateOfDiagnosisText = dateOfDiagnosis.getText().toString(); 
      String siteLocationText = locSpinner.getSelectedItem().toString(); 
      String wardClinicText = wardClinic.getText().toString(); 
      String historyText = "N"; 
      String physicalText = "N"; 

      if (history.isChecked()) { 
       historyText = "Y"; 
      } 
      if (physical.isChecked()) { 
       physicalText = "Y"; 
      } 

      if (patientInitialText.trim().equals("")) { 
       Toast.makeText(this, "Initial cannot be empty ", Toast.LENGTH_SHORT) 
         .show(); 
      } else if (wardClinicText.trim().equals("")) { 
       Toast.makeText(this, "Ward/Clinic Cannot be empty", 
         Toast.LENGTH_SHORT).show(); 
      } 



      return encounterValue; 
    } 


    public String retrieveProcedureValue() 
    { 
     String procedureValue = null; 
     LinearLayout ListProcedure =(LinearLayout) findViewById(R.id.procedureslistParent); 
     //Get the number of rows - Correct number of row. 
      for(int i = 0 ; i<ListProcedure.getChildCount();i++) 
      { 
       //get that row 's listV 
       LinearLayout listParent = (LinearLayout) ListProcedure.getChildAt(i); 
       //Nested LL is 2nd item of listParent. 
       LinearLayout nestedLL = (LinearLayout)listParent.getChildAt(1); 
       //Get the checkbox in that nested. 
       CheckBox cb = (CheckBox) nestedLL.getChildAt(0); 

       //If it's checked 
       if(cb.isChecked()) 
       { 
        //take the ID value of the checked and add it to encouterSelected array. 
        TextView idTV = (TextView) listParent.getChildAt(0); 
        procedureSelected.add(idTV.getText()); 
       } 
      } 
      //Sort the array on order before sending it back to NewCases 
      Collections.sort(procedureSelected); 
      int B = procedureSelected.size(); 
      if (B != 0) 
      { 
     StringBuilder full = new StringBuilder(); 
     procedureValue = full.append(procedureSelected).toString(); 
     procedureValue = procedureValue.replaceAll("\\s",""); 
      } 
      else 
      { 
       procedureValue = "No Procedure Selected"; 
      } 

      return procedureValue; 
    } 

    public void openProgress() 
    { 
     Intent progressIntent = new Intent(this, ScanSelected.class); 
     progressIntent.putExtra(EXTRA_MESSAGE, "MyProgress"); 
    startActivity(progressIntent); 
    } 

    public void openSaveCases() 
    { 
     Intent savedCaseIntent = new Intent(this, SavedCases.class); 
     startActivity(savedCaseIntent); 
    } 
} 

환자 정보 조각

public class PatientInfoFragment extends Fragment{ 

    public static final String ARG_SECTION_NUMBER = "section_number"; 

    // DATABASE ADAPTOR 
    patientDbAdapter patientDB; 
    Context myContext; 
    String ID = "0"; 

    // SPINNER AND SEARCH VIEW 
    private SearchView mSearchView; 
    private TextView mStatusView; 
    private Spinner locationSpinner; 

    // DATE SPINNER 
    private int mYear; 
    private int mMonth; 
    private int mDay; 

    private TextView mDateDisplay; 
    static final int DIALOG_ALERT = 1; 

    // declaration 
    private EditText patientInitial, dateOfDiagnosis, wardClinic; 
    private Spinner locSpinner; 
    private CheckBox history, physical; 
    private String siteLocation; 
    String patientKey = "com.example.app.datetime"; 
    // Verification 
    Boolean fromBase = false; 

    //Data From PatientDetails 
    String strValue1 ; 
    String strValue2 ; 
    String strValue3 ; 
    String strValue4 ; 
    String strValue5 ; 
    String strValue6 ; 
    private String patientID; 

    static final int DATE_DIALOG_ID = 0; 

    View rootView; 

    public PatientInfoFragment() { 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.activity_patient_info, 
       container, false); 

      history = (CheckBox) rootView.findViewById(R.id.checkbox_history); 
      physical = (CheckBox) rootView.findViewById(R.id.checkbox_physical); 
      patientInitial = (EditText) rootView.findViewById(R.id.InitialTextEdit); 
      locSpinner = (Spinner) rootView.findViewById(R.id.locationSpinner); 
      dateOfDiagnosis = (EditText) rootView.findViewById(R.id.showMyDate); 
      siteLocation = locSpinner.getSelectedItem().toString(); 
      wardClinic = (EditText) rootView.findViewById(R.id.WardClinicTextEdit); 

      // listener for the Ward/ClinicSpinner 
      addListenerOnSpinnerItemSelection(); 
      //mDateDisplay.setOnClickListener(new View.OnClickListener() { 
     //  public void onClick(View v) { 
       // showDatePicker(); 
      // } 
     // }); 
     return rootView; 
    } 


    private void RetrieveDataFromDatabaseBasedOnID(String paramString) { 
     myContext = getActivity(); 
     // Opening of database 
     patientDB = new patientDbAdapter(myContext); 
     patientDB.open(); 

     ID = paramString; 
     Cursor mCursor = this.patientDB 
       .retrievePatientEntriesBasedOnID(paramString); 
     if ((mCursor != null) && (mCursor.getCount() > 0)) { 
      mCursor.moveToFirst(); 

      mCursor.getString(0); // ID 
      this.strValue1 = mCursor.getString(1); // Initial 
      this.strValue2 = mCursor.getString(2);// Date 
      this.strValue3 = mCursor.getString(3);// SiteLocation 
      this.strValue4 = mCursor.getString(4);// WardClinic 
      this.strValue5 = mCursor.getString(5);// CheckBoxHistory 
      this.strValue6 = mCursor.getString(6);// CheckBoxPhysical 

      patientInitial = (EditText) rootView.findViewById(R.id.InitialTextEdit); 
      locSpinner = (Spinner) rootView.findViewById(R.id.locationSpinner); 
      dateOfDiagnosis = (EditText) rootView.findViewById(R.id.showMyDate); 
      siteLocation = locSpinner.getSelectedItem().toString(); 
      wardClinic = (EditText) rootView.findViewById(R.id.WardClinicTextEdit); 
      history = (CheckBox) rootView.findViewById(R.id.checkbox_history); 
      physical = (CheckBox) rootView.findViewById(R.id.checkbox_physical); 

      patientInitial.setText(strValue1); 
      dateOfDiagnosis.setText(strValue2); 
    //  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
    //    R.array.location_arrays, android.R.layout.simple_spinner_item); 
     // int S = adapter.getPosition(strValue3); 

     // locSpinner.setSelection(S); 
      wardClinic.setText(strValue4); 
      if (strValue5.equals("Y")) { 
       history.setChecked(true); 
      } 
      if (strValue6.equals("Y")) { 
       physical.setChecked(true); 
      } 
      fromBase = true; 
     } else { 
      fromBase = false; 
     } 
    } 

    // Listen to the spinner Item Selection - Completed 
    public void addListenerOnSpinnerItemSelection() { 
     locationSpinner = (Spinner) rootView.findViewById(R.id.locationSpinner); 
     // locationSpinner.setOnItemSelectedListener(new 
     // CustomOnItemSelectedListener()); 
    } 

    // Listen to the spinner 
    public void addListenerOnButton() { 
     // retrieveValue 
     locationSpinner = (Spinner) rootView.findViewById(R.id.locationSpinner); 
    } 

    public void openPatientSearch() { 
     Intent patientDetailsIntent = new Intent(myContext, PatientDetails.class); 
     //StartActivityForResult doesn't work for fragment 
    // PatientInfo.this.startActivityForResult(patientDetailsIntent, 1); 
    } 

PatientInfo의 XML. 유효한 뷰 또는 NPE "무작위"(따옴표) 중 하나를 얻는 것으로 나타 때문에

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/patientTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
     android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@drawable/bg" 
    android:shrinkColumns="1" > 

    <!-- 2 columns Row 2 --> 
    <!-- Patient Initials Row --> 
    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <TextView 
      android:id="@+id/InitialsTextView" 
      android:text="Patient Initials:" 
      android:textStyle="bold" 
       android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

      <EditText 
      android:id="@+id/InitialTextEdit" 
      android:layout_span="2" 
      android:singleLine="true" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
      android:inputType="textVisiblePassword" 
      android:imeOptions="actionNext" 
      android:hint="@string/patient_input" /> 

    </TableRow> 
    <!-- End Of row 1 --> 

    <!-- Row 2 --> 
    <!-- Date Of Diagnosis Row --> 
     <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <TextView 
      android:id="@+id/DiagnosisTextView" 
         android:textStyle="bold" 
      android:text="Date of Diagnosis:" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

      <EditText 
    android:id="@+id/showMyDate" 
    android:hint="Enter Date here" 
    android:editable="false" 
    android:focusableInTouchMode="false" 
    android:layout_height="wrap_content" 
     android:layout_weight="0.6"/> 
    </TableRow> 
<!-- End of row 2 --> 

    <!-- Row 3 --> 
    <!-- Date Of Diagnosis Row --> 
     <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <TextView 
      android:id="@+id/LocationTextView" 
         android:textStyle="bold" 
      android:text="Site/Location:" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <Spinner 
     android:id="@+id/locationSpinner" 
      android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
     android:entries="@array/location_arrays" 
     android:prompt="@string/DateSpinnerText" /> 

    </TableRow> 
<!-- End of row 3 --> 

    <!-- Row 4 --> 
    <!-- ward/clinic Row --> 
     <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <TextView 
      android:id="@+id/WardClinicTextView" 
         android:textStyle="bold" 
      android:text="Ward/Clinic:" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <EditText 
      android:id="@+id/WardClinicTextEdit" 
      android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
        android:singleLine="true" 
      android:hint="Good to include bed no." /> 

    </TableRow> 
<!-- End of row 4 --> 


    <!-- edittext span 2 column --> 
    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

    </TableRow> 

    <!-- just draw a red line --> 
    <View 
     android:layout_height="2dip" 
     android:background="#0000FF" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <CheckBox android:id="@+id/checkbox_history" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
        android:textStyle="bold" 
     android:text="Performed History" 
     /> 


     <CheckBox android:id="@+id/checkbox_physical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
        android:textStyle="bold" 
     android:text="Performed Physical" 
     /> 


    </LinearLayout> 

</TableLayout> 

답변

2

, 나는 ViewPager/Adapter 캐시/재활용 문제를 냄새. 호출기에 setOffscreenPageLimit을 설정하면 3 조각을 모두 포함 할 수 있습니다 (기본적으로 기본값은 1Fragment, IIRC).

제안 사항으로는 논리가 특정 조각 내부의보기와 관련이있는 데이터에 의존하기 때문에 관련 조각 안에 메뉴 옵션을 넣는 것이 좋습니다. 메뉴 옵션을 선택할 때 ViewPager에 캐싱되지 않을 수 있습니다.

단편 전용 메뉴 항목을 제공하는 것은 문제가 없으며, 단정 메뉴 항목을 투명하게 (즉, 단편이오고가는 것처럼) 호출 할 수 있기를 기대합니다.

+0

나는 의도를 시도하고 다음 활동으로 전달하기 전에 모든 세 조각에서 정보를 얻으려고합니다. 따라서, 나는 그것을 주 활동에 넣는 것이 좋은 생각이라고 생각했다. 또는 개별 조각에 대한 onCreateView에 배치해야하며 엔터티 클래스를 전달해야합니까? 엔터티 클래스에서 검색하기 전에? – Walalala

+0

아, 알겠습니다. 그러나 당신이하는 일은 그것이 작동하는 한 당신에게 달린 것입니다. 답변으로 문제가 해결 되었습니까? – davidcesarino

+0

예. 감사! – Walalala

관련 문제