Fragment中的getArguments()返回null

时间:2015-07-01 04:26:10

标签: android

我已经研究过已经回答的所有片段getArgument空指针问题,我似乎无法找到适合我的解决方案。我的片段运行正常(按钮执行正常功能),除了它没有传入任何参数。我已经设置了我的构造函数和我的各种调用,并且无法避免NullPoint。我认为它必须是片段生命周期的一部分,并且屏幕上出现的片段某种程度上不是发起的片段,但我无法触及它的底部。任何帮助将不胜感激!

片段代码:

public class BottomBar extends Fragment {

ImageButton goHome, goPending, goActive, goHelp, goChallenge;
BottomListener activityCallback;
int chalType;
String title;
String text;

//constructor log.d prints out that it has been reached
public static BottomBar init(int chalType, String ttl, String txt){
    BottomBar bot = new BottomBar();
    Bundle args = new Bundle();
    args.putInt("chalType", chalType);
    args.putString("title", ttl);
    args.putString("text", txt);
    bot.setArguments(args);
    Log.d("Bottom Bar Init", "called");

    return bot;
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
        activityCallback = (BottomListener) activity;
    } catch (ClassCastException e){
        Log.d("bottom bar onAttach","class cast");
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.bottombarfrag, container, false);
    goHome = (ImageButton) view.findViewById(R.id.goHome);
    goPending = (ImageButton) view.findViewById(R.id.goPending);
    goChallenge = (ImageButton) view.findViewById(R.id.challenge);
    goActive = (ImageButton) view.findViewById(R.id.goActive);
    goHelp = (ImageButton) view.findViewById(R.id.getInfo);

    Bundle argsin = getArguments();

//never enters this loop... argsin always null
    if(argsin!=null){ 
        Log.d("bottombar oncreate", "argsin found");
    chalType = argsin.getInt("chalType", 0);
    title = argsin.getString("title");
    text = argsin.getString("text");
    }

    //.... button activities, functioning properly ....

    return view;
}

和调用它的主要活动代码:

public class MainActivity extends FragmentActivity {

Button btn, paydemo;
TextView tv1, tv2;

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

    String ttl = "Trial1 Home";
    String txt = "string1text";
    BottomBar bot = BottomBar.init(0, ttl, txt);
    getSupportFragmentManager().beginTransaction().add(android.R.id.content, bot).commit();

    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.clickbtn);
    paydemo = (Button) findViewById(R.id.PayPalDemo);
    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView3);
    tv1.setText("try1");
    tv2.setText(""); 
    // ...other button actions       
}

跟进: 我发现我可以获得我想要的片段,但是它看起来像是片段上的第二个实例(位于显示的顶部,而不是所需的位置),原始实例在那里,但功能不正常(仍然没有args)。我认为当我在我的活动中初始化片段时,我不会覆盖由xml添加的现有片段。也许我需要初始化支持片段管理器并将其链接到正确的实例来替换它?想法?

xml最初预期的片段所在的位置(这是我想要的片段,但不是与之通信的片段)。

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second Layout"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/retBut"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Return" />


<fragment
    android:id="@+id/bottombar"
    android:name="com.example.trial1.BottomBar"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="0dp"

    />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

这个很棘手。看看你的活动:你正在设置片段,然后调用setContentViewsetContentViewandroid.R.id.content删除所有视图,我认为这会以某种方式打破片段的生命周期。此外,它看起来像一个概念错误:设置片段并在此之后擦除它。我建议在R.layout.activity_main内部创建一个片段容器,设置活动内容,之后将片段放入此容器中。

答案 1 :(得分:0)

所以我想出了一个有效的答案。我不相信它是正确的方法,但它确实有效。

我为引用此片段的所有类创建了一个继承类,并创建了一组公共变量来表示传递给片段的参数。然后我让每个类在超类中编写公共变量,而不是启动一个新的片段。然后该片段引用相同的公共变量来从中提取信息。

同样,不要认为这是正确的方法 - 传递的论点存在效率 - 但它现在对我有用。如果有人对我如何能够实现这一目标有更好的了解,那我就听见了。

感谢所有帮助