简体   繁体   English

Intent和putExtra不起作用

[英]Intent and putExtra does not work

I'm going crazy with Android programming. 我对Android编程很疯狂。 Nothing works properly.. 没有什么工作正常..

Whats wrong with this? 这有什么不对吗?

Error: getIntent() is undefined for type View 错误: getIntent() is undefined for type View

Any ideas? 有任何想法吗?

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

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

        TextView text = (TextView)rootView.findViewById(R.id.ttv);

        Bundle intentBundle = rootView.getIntent().getExtras();
        int question_cat = intentBundle.getInt("question_cat");

        text.setText(question_cat);

        return rootView;
    }
}

You can just do: 你可以这样做:

Intent intentBundle = getActivity().getIntent();
        String question_cat = intentBundle.getStringExtra("question_cat");
        Log.i("Result : ", question_cat);

Even after you get the value as string, you use it as a int value later like this : 即使在将值作为字符串获取之后,您也可以像以下一样将其用作int值:

int j = Integer.valueOf(question_cat);

    Log.i("Result : ", String.valueOf(j));

For your other question, with getIntent(), the problem is that you are using it inside the fragment class and in order to use that, you have to use getActivity() to access it. 对于您的其他问题,使用getIntent(),问题是您在片段类中使用它,并且为了使用它,您必须使用getActivity()来访问它。 If it was just a normal activity, it wasn't that complicated. 如果这只是一项正常的活动,那就不那么复杂了。 Android is really fun if some concepts are clear .. :) 如果一些概念清楚,Android真的很有趣.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM