FindViewByID Fragment Null

时间:2014-01-11 02:22:05

标签: java android android-fragments

我遇到了一些问题。我似乎随机从null获得findViewById()返回。有时会返回TextViewLinearLayout ..有时它只会给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。

<?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>

1 个答案:

答案 0 :(得分:2)

由于您似乎获得了有效的观看次数或NPE“随机”(引号),我闻到了ViewPager / Adapter缓存/回收问题。在寻呼机中设置setOffscreenPageLimit以包含所有3个片段(默认情况下,默认为1 Fragment到任何一方,IIRC)应该解决它。

作为一个建议,我建议将菜单选项放在相关的片段中,而不是放在你的Activity里面,因为逻辑取决于与特定片段内的视图相关的数据,这可能会或可能在您选择菜单选项时,不要在ViewPager中缓存。

我应该注意,提供仅限片段的菜单项是完美的,并且期望它们与寻呼机透明地工作(即,它们随着片段的来来去去)。