片段从意图返回null(Fragment returning null from intent)

2019-11-03 21:53发布

所以我做了一堆的4个按钮,把意图给他们每个人。 他们都定位到同一个片段类。 但他们的演员是不同的,所以如果被点击了按钮1,片段会打开并做了一定的作用,如果被点击按钮2,碎片会做其他的动作等。 我试图在正常活动的代码和它的工作,但在片段它不工作。 它只是返回我“ID为空”

课堂上发的意图

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

片接收的意图和群众演员

public class ItemDetailFragment extends Fragment {
public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_3 = 3;
public static final int ACTION_4 = 4;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";


public static final String ARG_ITEM_ID = "item_id";


private DummyContent.DummyItem mItem;

public ItemDetailFragment() {
}

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


    if (getArguments().containsKey(ARG_ITEM_ID)) {
        // Load the dummy content specified by the fragment
        // arguments. In a real-world scenario, use a Loader
        // to load content from a content provider.
        mItem = 
DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
    }


    int id = getActivity().getIntent().getIntExtra(ID_ACTION, -1);

    if (id == ACTION_NULL) {
        Log.d("TAG", "id is null");
        Toast.makeText(getActivity(), "id is null!", 
Toast.LENGTH_SHORT).show();
    } else if (id == ACTION_1) {
        Log.i("TAG", "ALLOHA! from button 1");
        Toast.makeText(getActivity(), "Aloha from button 1!", 
Toast.LENGTH_LONG).show();
    } else if (id == ACTION_2) {
        Log.i("TAG", "Hello from button 2");
        Toast.makeText(getActivity(),"Hello from button 2!", 
Toast.LENGTH_LONG).show();
    }
    else if (id == ACTION_3) {
        Log.i("TAG", "Hello from button 3");
        Toast.makeText(getActivity(),"Hello from button 2!", 
Toast.LENGTH_LONG).show();
    }
    else if (id == ACTION_4) {
        Log.i("TAG", "Hello from button 4");
        Toast.makeText(getActivity(),"Hello from button 2!", 
Toast.LENGTH_LONG).show();
    }


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail, 
container, false);



    return rootView;
}
}

Answer 1:

要导航到一个片段,您需要实例片段类,然后使用FragmentManager进入到一个交易:

FragmentClass fragment = new FragmentClass();

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

您可以继续就在当前活动的每一个动作(如它保持主要活动)。



Answer 2:

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


文章来源: Fragment returning null from intent