片段从intent返回null

时间:2015-07-02 20:02:47

标签: java android android-fragments android-intent extras

所以我制作了一堆4个按钮,对每个按钮都有意图。它们都导航到相同的Fragment类。但是他们的额外内容是不同的,所以如果点击了button1,Fragment会打开并执行某个操作,如果单击button2,Fragment会执行另一个操作,依此类推。我尝试了正常活动的代码并且它有效,但是在片段中它不起作用。它只是让我“Id is null”

发送意图的类

alpha • a | b | c | d
beta • h | i | j | k
gamma • m | n | o
delta • p | q | r | s
zeta • x | y | z ; comment here

收到意图和附加内容的片段

public class Intennt extends ActionBarActivity {

Button bt1,bt2,bt3,bt4;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intennt);
    bt1 = (Button) findViewById(R.id.button);
    bt2 = (Button) findViewById(R.id.button2);
    bt3 = (Button) findViewById(R.id.button3);
    bt4 = (Button) findViewById(R.id.button4);


    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, ItemListActivity.class);

i.putExtra(ItemDetailFragment.ID_ACTION,ItemDetailFragment.ACTION_1);
            startActivity(i);
        }
    });

    bt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, ItemListActivity.class);  
            i.putExtra(ItemDetailFragment.ID_ACTION, 
  ItemDetailFragment.ACTION_2);
            startActivity(i);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

要导航到片段,您需要实例化片段类,然后使用FragmentManager进行事务处理:

FragmentClass fragment = new FragmentClass();

getFragmentManager()    
    .beginTransaction()
    .replace(R.id.your_fragment_view, fragment)
    .commit();

您可以继续执行当前活动中的每个操作(因为它仍然是主要活动)。

答案 1 :(得分:0)

public class Questions extends DialogFragment {

private static Button Submit;
public int count = 0;

public interface DialogListener {
    void onQuestionsFinish(ArrayList<xmlQuestion> l);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_questions, container, false);
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);



    Submit = (Button) rootView.findViewById(R.id.SubmitButton);


    Submit.setOnClickListener(new View.OnClickListener(){
        public void onClick(View arg0){
            for(int i = 0; i<questionList.size(); i++)
            {
                questionList.get(i).setAnswer();
            }
            DialogListener activity = (DialogListener) getActivity();
            activity.onQuestionsFinish(questionList);
            Questions.this.dismiss();
        }
    });

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    yInch = metrics.ydpi;
    pixHeight = metrics.heightPixels;

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point(); display.getSize(size);
    screenWidth=size.x; screenHeight=size.y;

    LinearLayout.LayoutParams bodyParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,screenHeight*900/1024);
    BodyLayout.setLayoutParams(bodyParams);

    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
    wmlp.height=screenHeight; wmlp.width=screenWidth;

    getDialog().getWindow().setAttributes(wmlp);
    WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
    lp.dimAmount=0.4f;
    getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    getDialog().setCanceledOnTouchOutside(true);


    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_FRAME, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);

}


public Questions()
{

}

}

您不必使用意图来启动片段。使用片段的主要好处是能够更容易地引用每个片段中的数据。您可以简单地创建片段类的新实例,并分配它的公共变量。然后就这样开始......

Questions q = new Questions();
q.count = 0;
q.show(getSupportFragmentManager(), "Dialog Fragment");