问题描述
我有一个Android应用程序。其中有两个片段一个活动。
I have an android application. Which have one activity with two fragments.
在我的应用程序启动时将其分配到的数据在他的活动的变量。然后在一个按钮第二个片段的点击打开,从他的活动是在活动指定需要的数据。但它返回null。我已经试过到目前为止...
When my application starts it assigns data to a variable in his activity. Then on the click of a button second fragment opens and it takes data from his activity which is assigned in activity. But it returns null. What I have tried so far is...
我在活动课可变从我的片段是这样分配的数据。
I made variable in my activity class and assign data from my fragment like this.
((MainActivity)getActivity()).resultObj = gson.fromJson(HomeCardString.toString(), HomeCardResult.class);
和它完美地分配为我已经在这样同一片段中使用这些数据。
and it assigned perfectly as i have used this data in same fragment like this
HomeCardListItemAdapter adapter = new HomeCardListItemAdapter(getActivity(),
R.layout.home_cardlist_item, ((MainActivity)getActivity()).resultObj.HomeData.FriendSummary);
这完美的作品。
但是,当我打开我的第二个片段上一点击,它只是显示了我的布局不执行任何重写的方法。像oncontentview等,由于这些所有的方法都在活动开始执行。但当时我没有数据分配给我的活动的变量。所以,我做了我自己的方法,并显示出这样我的第二个片段之后调用它。
But when I opened my second fragment on a click, it just shows my the layout without executing any of its override method. like oncontentview etc, As these all method were executed at the start of activity. But at that time i haven't assigned the data to my activity's variable. So i made my own method and call it right after showing my second fragment like this.
showFragment(FRIENDLIST, true);
FriendListActivity asdss = new FriendListActivity(); // this is my second fragment
asdss.populateFriendList();
而在第二个片段我有这个code的方法。
And in second fragment I have that method with this code.
public void populateFriendList()
{
FriendList = ((MainActivity)getActivity()).resultObj.HomeData.Friends;
String asd = FriendList.toString();
asd = asd + "asdasda";
}
但经过[好友列表=((MainActivity)getActivity())resultObj.HomeData.Friends;。这条线就显示了编写的源文件invocationtargetexception.class在里面没有找到
But after [FriendList = ((MainActivity)getActivity()).resultObj.HomeData.Friends;] this line it shows invocationtargetexception.class file with written source not found in it.
和在logcat中出现此错误。
And in logcat this error appears.
12月7日至24日:06:47.204:E / AndroidRuntime(3926):显示java.lang.NullPointerException
07-24 12:06:47.204: E/AndroidRuntime(3926): java.lang.NullPointerException
推荐答案
OK你这是怎么从活动将数据发送到您的片段
OK this is how you send data to your fragments from activity
(In your Activity)
// You create a bundle
Bundle bundle = new Bundle();
// Add data to your the bundle
bundle.putInt("key", your_data); //e.g. adding integer, you can do similar for other data types
// Pass bundle to the fragment using setArguments()
your_fragment.setArguments(bundle);
(In your Fragment class)
Bundle bundle = getArguments();
Integer your_data = bundle.getInt("key", default_value);
请注意:你甚至可以传递对象片段。为此你需要让你的对象类Parcelable并调用 bundle.putParcelable(钥匙,your_object);
玩弄可以传递给片段什么样的数据类型。请不要问,如果你被卡住任何形式的问题。
Play around with what sort of data types you can pass to fragments. Please do ask if you get stuck on any sort of problem.
这篇关于从片段分配数据到活动变量,而在另一个片段获得可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!