オブジェクトをアクティビティからフラグメントに渡したいと思います。私のアクティビティには、ビューページャーと4つのフラグメントからなるタブレイアウトがあります。オブジェクトをアクティビティからフラグメントにアクセスする方法は?
私はすべてのフラグメントでアクセスしたいアクティビティのオブジェクトを持っています。
私はアクティビティでgetMethodを作成し、フラグメント内のオブジェクトにアクセスしようとしました。しかし、それは動作していない、私はnullとしてオブジェクトを取得しているすべての方法。
活動:私はフラグメントにプロファイルオブジェクトを渡したい
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Profile");
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
GetProfileAsyncTask task = new GetProfileAsyncTask(ProfileActivity.this,this,profile);
// task.setTaskCompletedListener(this);
task.execute("sid");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mTitles,mNumbOfTabs);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabs = (SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tab_scroll_color);
}
});
mTabs.setViewPager(mViewPager);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public JSONObject getProfile(){
return this.profile;
}
@Override
public void doInBackground() {
}
@Override
public void doPostExecute(JSONObject response) {
profile = response;
Toast.makeText(ProfileActivity.this,
"Finish", Toast.LENGTH_LONG).show();
}
。
フラグメント:
public class BasicInformationFragment extends android.support.v4.app.Fragment{
EditText email,pass,mobile,code;
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
JSONObject profile;
String userUsername,password,mobileNumber,emailId;
private ProfileActivity myActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_basic_information,
container, false);
// try{
((ProfileActivity)getActivity()).getProfile();
// password = profile.getString("userPassword");
// mobileNumber = profile.getString("userMobileNo");
// emailId = profile.getString("userEmailid");
email = (EditText) view.findViewById(R.id.edt_email);
pass = (EditText) view.findViewById(R.id.edt_pass);
mobile = (EditText) view.findViewById(R.id.edt_mobile);
code = (EditText) view.findViewById(R.id.edt_code);
// email.setText(emailId);
// pass.setText(password);
// mobile.setText(mobileNumber);
// }
// catch (JSONException e)
// {}
return view;
}
ここで何が間違っているだろうか?助けてください。ありがとうございます..
基本的に私は断片のアクティビティのオブジェクトにアクセスしたいと思います。別のアプローチがある場合は私に教えてください。ありがとう
'((ProfileActivity)getActivity())。getProfile();' – Lawrance
を呼び出すときにエラーが発生しました。私がデバッグするとき、オブジェクトはnullです。アクティビティの@Lawrance –
はAsyncTaskの完了後にプロファイルオブジェクトを取得していますか? – NehaK