setArgument无法在片段中传递数据

时间:2014-06-04 19:18:13

标签: android android-fragments

我想在两个片段之间传递数据,

这是我的setArgument

ListFragment fragment = new ListFragment();
Bundle bundle = new Bundle();

bundle.putString("Test", "test");
fragment.setArguments(bundle);

这是我的getArgument

在我的onCreate方法

String test = args.getString("Test","test");

if(this.getArguments()!=null)
{
    Log.d("hey", "hey");
    test = this.getArguments().getString("Test","test");
    details_id.setText(test);
}

java.lang.NullPointerException

details_id.setText(test);

我的争论正在发挥作用我可以嘿嘿:嘿,我的日志,但我的setArgument无效。

  

更新

我现在没有任何错误,但我无法传递任何论据

这是我更新的onCreate方法

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    chooseTrip(tripIds.get(position));

    String selectedTripId = tripIds.get(position);

    Helpers.saveToSharedPreferences(getActivity(),
            Constants_Prefs.SELECTED_TOP_LEVEL_RECORD,
            Constants_Keys.SELECTED_TRIP_ID,
            selectedTripId);

    ListFragment fragment = new ListFragment();
    Bundle bundle = new Bundle();

    bundle.putString("Test", "test");
    fragment.setArguments(bundle);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View fragV = inflater.inflate(R.layout.fragment_main, container, false);
    listView = (ListView)fragV.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);

    TextView details_id = (TextView)fragV.findViewById(R.id.detail_header_id);
    details_id.setVisibility(View.VISIBLE);
    if(this.getArguments()!=null){
        Log.d("hey", "hey");
        test = this.getArguments().getString("Test","t");
        details_id.setText(test);
    }
}

我的日志:嘿:嘿

我的textView打印: 如果我使用test = this.getArguments().getString("Test","t"); - &gt;我的textView

如果我使用test = this.getArguments().getString("Test"); - &gt;我的textView没什么

1 个答案:

答案 0 :(得分:1)

不是你的getArguments()无效。您在details_id.setText(test)中呼叫onCreate(Bundle savedInstanceState)。我假设details_idTextView

您必须在onCreateView方法中初始化片段的视图,该方法在系统onCreate之后调用

========== EDIT =======

在你的片段中试试这个:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View fragV = inflater.inflate(R.layout.fragment_main, container, false);
    listView = (ListView)fragV.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);


    TextView details_id = (TextView)fragV.findViewById(R.id.detail_header_id);
    details_id.setVisibility(View.VISIBLE);
    if(this.getArguments()!=null){
        Log.d("hey", "hey");
        test = this.getArguments().getString("Test","t");
        details_id.setText(test);
    }
    return fragV;
}

在创建片段的活动中尝试此操作:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ListFragment fragment = new ListFragment();
    Bundle bundle = new Bundle();

    bundle.putString("Test", "test");
    fragment.setArguments(bundle);
    showFragment(fragment, true);
}

private void showFragment(Fragment frag, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();


    transaction.replace(R.id.content_frame, frag);
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}

你的main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
<LinearLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>