2

I have an activity which contains a fragment. The fragment is successfully attached in the onAttach() method (checked it). When I then press a button in the fragment I get a NPE on performing getActivity. After reading several SO-questions I found out that by fragment has obviously been detached from my activity. But I don't understand the several answeser and even the android docu how to solve this problem :(

My question now is: How can I reattach my fragment to avoid the NPE?

My acitivity:

public class SearchActivity extends AppFragmentActivity {
  SearchFragment searchfragment;

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

    searchfragment = new SearchFragment();
  }


  public void ClickBtnSearch(View view) {
    // Send down to fragment
    searchfragment.ClickBtnSearch(view);


  }
}

It's XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <fragment android:name="de.unibonn.sdb.wissappmobile.fragments.SearchFragment"
              android:id="@+id/search_fragment"
              android:layout_weight="1"
              android:layout_width="0px"
              android:layout_height="match_parent" />


</LinearLayout> 

The fragment:

public class SearchFragment extends Fragment {

  @Override
  public void onResume() {
    super.onResume();
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_search, container, false);
  }

  @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }   


  public void  ClickBtnSearch(View view) {

    // Read entered ID and then start search
    String enteredString = "";
    int LabeledObjectID = 0;

    // THE FOLLOWING LINE produces NPE in the getActivity !
    TextView txtError = (TextView) getActivity().findViewById(R.id.txtError);
    EditText enteredValue = (EditText) getActivity().findViewById(R.id.dfnLabeledObjectID);

    if (!enteredValue.getText().toString().equals("")) {
      enteredString = enteredValue.getText().toString();

      // Acutally we don't need a try/catch to catch a NumberFormatException as the Inputfield is numeric only #
      // and doesn't accept alphanumeric values or floats. 
      try {
        LabeledObjectID = Integer.valueOf(enteredString);

        // TODO Suche starten
        txtError.setText("the entered ID is" + LabeledObjectID);

      } catch (NumberFormatException e) {
        txtError.setText(R.string.ERR_Please_Enter_a_nonempty_numeric_ID);
      }
    } else {
      txtError.setText(R.string.ERR_Please_Enter_a_nonempty_numeric_ID);
    }       
  }
}

and the fragment's XML button definiton:

<Button
    android:id="@+id/btnSearch"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_below="@id/dfnLabeledObjectID"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="20dp"
    android:onClick="ClickBtnSearch"
    android:text="@string/BTN_SEARCH"
    android:textAlignment="center" />
4

1 回答 1

0

好吧,您根本没有将片段附加到活动中。

你需要这样称呼:

FragmentManager manager=getFragmentManager();
String tag=SearchFragment.getCanonicalName();
SearchFragment fragment=manager.findFragmentByTag(tag);
if(fragment==null)
   fragment= new SearchFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
SearchFragment fragment= new SearchFragment();
fragmentTransaction.replace(R.id.fragmentContainer, fragment,tag);
fragmentTransaction.commit();
于 2013-08-13T16:32:55.517 回答