该程序有一个$date->_month_length($date->year,$date->month);
类,它接收来自DisplayActivity
类的数据。 LoginActivity
有一个DisplayActivity
来显示从Fragment
传递的数据。
问题是LoginActivity
类接收的Bundle
为null。因此数据未显示在DisplayFragment
中。我附上了代码。请强调错误。
Activity
课程 -
DisplayActivity
public class DisplayActivity extends AppCompatActivity {
private Toolbar toolbar;
private Button editButton;
private static Bundle infoBundle;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_display);
infoBundle = getIntent().getExtras();
DisplayFragment details = new DisplayFragment();
details.setArguments(infoBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_details, details).commit();
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("DisplayScreen");
editButton = (Button) findViewById(R.id.edit_button);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent backIntent = new Intent(DisplayActivity.this, LoginActivity.class);
backIntent.putExtras(infoBundle);
startActivity(backIntent);
finish();
}
});
}
课程 -
DisplayFragment
答案 0 :(得分:0)
我提供了LoginActivity,DisplayActivity和DisplayFragment的完整代码,其中DisplayFragment接收通过DisplayActivity从LoginActivity传递的值。试试这个
LoginActivity:
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.btnSubmit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("name", "My name is ABCD");
Intent i = new Intent(LoginActivity.this, DisplayActivity.class);
i.putExtras(bundle);
startActivity(i);
}
});
}
}
DisplayActivity:
public class DisplayActivity extends AppCompatActivity {
private Button editButton;
private Bundle infoBundle;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_display);
infoBundle = getIntent().getExtras();
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_container);
/*infoBundle = new Bundle();
infoBundle.putString("name", "ABCD");*/
DisplayFragment details = new DisplayFragment();
details.setArguments(infoBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, details).commit();
}
}
和DisplayFragment:
public class DisplayFragment extends Fragment {
public static Bundle backBundle;
private TextView mTextView, tvValue;
public DisplayFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
backBundle = getArguments();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_details, container, false);
if (backBundle != null) {
String username = "";
username = backBundle.getString("name");
tvValue = (TextView) inflatedView.findViewById(R.id.tvValue);
tvValue.setText(username);
}
return inflatedView;
}
@Override
public void onResume() {
super.onResume();
}
}
请在AndroidManifest.xml中为LoginActivity和DisplayActivity添加权限