片段的意图返回null [英] Fragment returning null from intent

查看:234
本文介绍了片段的意图返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

发送级的意图

 公共类Intennt扩展ActionBarActivity {按钮BT1,BT2,BT3,BT4;
上下文的背景下=这;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_intennt);
    BT1 =(按钮)findViewById(R.id.button);
    BT2 =(按钮)findViewById(R.id.button2);
    BT3 =(按钮)findViewById(R.id.button3);
    BT4 =(按钮)findViewById(R.id.button4);
    bt1.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            意图I =新意图(背景下,ItemListActivity.class);i.putExtra(ItemDetailFragment.ID_ACTION,ItemDetailFragment.ACTION_1);
            startActivity(ⅰ);
        }
    });    bt2.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            意图I =新意图(背景下,ItemListActivity.class);
            i.putExtra(ItemDetailFragment.ID_ACTION,
  ItemDetailFragment.ACTION_2);
            startActivity(ⅰ);
        }
    });
}

片段接收的意图和额外

 公共类ItemDetailFragment扩展片段{
公共静态最终诠释ACTION_1 = 1;
公共静态最终诠释ACTION_2 = 2;
公共静态最终诠释ACTION_3 = 3;
公共静态最终诠释ACTION_4 = 4;
公共静态最终诠释ACTION_NULL = -1;
公共静态最后弦乐ID_ACTION =的action_id;
公共静态最后弦乐ARG_ITEM_ID =ITEM_ID;
私人DummyContent.DummyItem MITEM;公共ItemDetailFragment(){
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    如果(getArguments()。的containsKey(ARG_ITEM_ID)){
        //加载的片段指定的虚拟内容
        //参数。在真实的场景中,使用Loader
        //从内容提供商加载内容。
        MITEM =
DummyContent.ITEM_MAP.get(getArguments()的getString(ARG_ITEM_ID));
    }
    INT ID = getActivity()getIntent()getIntExtra(ID_ACTION,-1)。    如果(ID == ACTION_NULL){
        Log.d(TAG,ID为空);
        Toast.makeText(getActivity(),ID为空!
Toast.LENGTH_SHORT).show();
    }否则如果(ID == ACTION_1){
        Log.i(TAG,ALLOHA从按钮1!);
        Toast.makeText(getActivity(),阿罗哈从按钮1!
Toast.LENGTH_LONG).show();
    }否则如果(ID == ACTION_2){
        Log.i(TAG,你好,从按钮2);
        Toast.makeText(getActivity(),你好,从按钮2!,
Toast.LENGTH_LONG).show();
    }
    否则,如果(ID == ACTION_3){
        Log.i(TAG,你好,从按钮3);
        Toast.makeText(getActivity(),你好,从按钮2!,
Toast.LENGTH_LONG).show();
    }
    否则,如果(ID == ACTION_4){
        Log.i(TAG,你好,从按钮4);
        Toast.makeText(getActivity(),你好,从按钮2!,
Toast.LENGTH_LONG).show();
    }
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment_item_detail,
集装箱,FALSE);    返回rootView;
}
}


解决方案

 公共类问题扩展DialogFragment {私有静态按钮提交;
公众诠释计数= 0;公共接口DialogListener {
    空隙onQuestionsFinish(的ArrayList&所述; xmlQuestion→1);
}
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment_questions,集装箱,FALSE);
    。getDialog()requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);    提交=(按钮)rootView.findViewById(R.id.SubmitButton);
    Submit.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(查看为arg0){
            的for(int i = 0; I< questionList.size();我++)
            {
                questionList.get(ⅰ).setAnswer();
            }
            DialogListener活性=(DialogListener)getActivity();
            activity.onQuestionsFinish(questionList);
            Questions.this.dismiss();
        }
    });    DisplayMetrics指标=新DisplayMetrics();
    。getActivity()getWindowManager()getDefaultDisplay()getMetrics(指标)。;
    yInch = metrics.ydpi;
    pixHeight = metrics.heightPixels;    显示显示= getActivity()getWindowManager()getDefaultDisplay()。
    点大小=新点(); display.getSize(大小);
    屏幕宽度= size.x; screenHeight = size.y;    LinearLayout.LayoutParams bodyParams =新LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,screenHeight * 900/1024);
    BodyLayout.setLayoutParams(bodyParams);    WindowManager.LayoutParams wmlp = getDialog()getWindow()的getAttributes()。
    wmlp.height = screenHeight; wmlp.width =屏幕宽度;    getDialog()getWindow()setAttributes(wmlp)。
    WindowManager.LayoutParams LP = getDialog()getWindow()的getAttributes()。
    lp.dimAmount = 0.4f;
    。getDialog()getWindow()addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    。getDialog()setCanceledOnTouchOutside(真);
    返回rootView;
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setStyle(DialogFragment.STYLE_NO_FRAME,android.support.v7.appcompat.R.style.Theme_AppCompat_Light);}
公共问题()
{}}

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

 问题解答Q =新问题();
q.count = 0;
q.show(getSupportFragmentManager(),对话片段);

So i made a bunch of 4 buttons , put an intent to each of them . They all navigate to the same Fragment class . But their extras are different, so if button1 was clicked , Fragment would open and do a certain action , if button2 was clicked , Fragment would do another action and so on. I tried the code on normal activities and it worked , but in fragments its not working . It just returns me "Id is null"

Class sending the intent

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

Fragment receiving the intent and extras

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

解决方案

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()
{

}

}

You don't have to use intents to start fragments. My main benefit for using fragments is being able to reference the data in each one much easier. You can simply create a new instance of your fragment class, and assign it's public variables. Then start it like so...

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

这篇关于片段的意图返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆