如何重新附加片段

时间:2013-08-13 16:22:38

标签: android android-fragments

我有一个包含片段的活动。该片段已成功附加到onAttach()方法中(已选中)。当我按下片段中的按钮时,我会在执行getActivity时获得NPE。在阅读了几个SO问题后,我发现片段显然与我的活动脱节了。但我不明白几个answeser甚至android文件如何解决这个问题:(

我现在的问题是:我如何重新连接我的片段以避免NPE?

我的意见:

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);


  }
}

这是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> 

片段:

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);
    }       
  }
}

和片段的XML按钮定义:

<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" />

1 个答案:

答案 0 :(得分: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();