您好我在那里查看了此错误解决方案的各种来源 包含 : Send data from activity to fragment using Tab Layout with Swipeable Views in Android
但我仍然坚持这个
我使用本教程创建了一个带有可滑动视图的选项卡布局。我试图将一个字符串从Activity传递给Fragment。我读了片段通信和关于stackoverflow的其他主题,但我仍然得到空指针异常。这是我的代码:
TabMainActivity.java
public class TabMainActivity extends FragmentActivity implements
ActionBar.TabListener,OnButtonPressListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
String email = "tv.nguyen.2011@sis.smu.edu.sg";
Bundle bundle=new Bundle();
bundle.putString("email", email);
MyLockerFragment fragobj=new MyLockerFragment(bundle);
fragobj.setArguments(bundle);; // This line is needed...
android.support.v4.app.FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.pager, fragobj);
transaction.addToBackStack(null);
transaction.commit();
}
}
main_menu.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
类 MyLockerFragment
public class MyLockerFragment extends Fragment {
String email =null;
Bundle bundle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
bundle = this.getArguments();
if(bundle != null)
{
email=bundle.getString("email");//line 55
}
Log.d("MyEmail_27: ", "> " + email);
......
}
我得到的错误是第55行最终为空
logcat的:
01-19 15:22:49.460:D / MyEmail_27:(14438):&gt; tv.nguyen.2011@sis.smu.edu.sg
01-19 15:22:49.495:D / MyEmail_27:(14438):&gt;空
MyEmail_27:(14438):&gt;空
MyEmail_27:(14438):&gt;空
错误:第55行电子邮件变量最终仍为空 请帮帮我