在Android中使用带可切换视图的选项卡布局将数据从活动发送到片段

在Android中使用带可切换视图的选项卡布局将数据从活动发送到片段,android,android-activity,android-fragments,nullpointerexception,bundle,Android,Android Activity,Android Fragments,Nullpointerexception,Bundle,我用这个创建了一个带有可切换视图的选项卡布局。我正在尝试将字符串从活动传递到片段。我阅读了关于stackoverflow的文章和其他一些主题,但仍然发现了空指针异常。这是我的密码: MainActivity.java public class MainActivity extends FragmentActivity { private static final int REQUEST_CODE_EMAIL = 1; String password, email; Vi

我用这个创建了一个带有可切换视图的选项卡布局。我正在尝试将字符串从活动传递到片段。我阅读了关于stackoverflow的文章和其他一些主题,但仍然发现了空指针异常。这是我的密码:

MainActivity.java

public class MainActivity extends FragmentActivity {
    private static final int REQUEST_CODE_EMAIL = 1;
    String password, email;

    ViewPager viewPager;
    TabsPagerAdapter tabsPagerAdapter;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);
                    }
                });
        viewPager.setAdapter(tabsPagerAdapter);
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }
        };
        actionBar.addTab(actionBar.newTab().setText("Głowna").setTag("main").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Rachunki").setTag("bills").setTabListener(tabListener));

        Bundle bundle=new Bundle();
        bundle.putString("email", "emasiofnasdbjk");
        EnterExitFragment enterExitFragment = new EnterExitFragment();
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.pager, enterExitFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
activity_main.xml

<android.support.v4.view.ViewPager 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>
enterextfragment.java

public class EnterExitFragment extends Fragment{
    TextView tvFloor1, tvFloor2, tvEmail;
    Button btnSend;
    Integer floorId, segmentId, spaceId, ticketId;
    String floorName, segmentName, spaceName;
    String email;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_enter_exit, container, false);

        tvEmail = ((TextView)rootView.findViewById(R.id.textEmail));
        tvFloor1 = ((TextView)rootView.findViewById(R.id.textFloor1));
        tvFloor2 = ((TextView)rootView.findViewById(R.id.textFloor2));
        new FloorFreeSpaces(EnterExitFragment.this).execute();

        btnSend = ((Button)rootView.findViewById(R.id.btnSend));
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Floor(EnterExitFragment.this).execute();
                email = getArguments().getString("email"); //line 55
                tvEmail.setText(email);
            }
        });
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
活动\u enter\u exit.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textFloor1"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textFloor2"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="354dp"
        android:layout_height="93dp"
        android:text="Email:"
        android:id="@+id/textEmail"
        android:layout_gravity="center_horizontal|bottom" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="SEND"
        android:id="@+id/btnSend"
        android:layout_gravity="center_horizontal|bottom" />

    </LinearLayout>

有人能解决我的问题吗?:)

EDIT:我的建议是创建以下构造函数,它将
Bundle
对象作为参数:

public EnterExitFragment(Bundle bundle) {
    super();
    setArguments(bundle);
}
并使用
bundle
对象调用该构造函数作为参数,而不是程序中的默认参数。这样,在将包附加到活动之前,您一定要添加包

此外,如果这不起作用,请向EEF类添加一个
Bundle
属性,在构造函数中初始化它并引用它,而不是
getArguments()
方法:

public class EnterExitFragment extends Fragment{
    TextView tvFloor1, tvFloor2, tvEmail;
    Button btnSend;
    Integer floorId, segmentId, spaceId, ticketId;
    String floorName, segmentName, spaceName;
    String email;
    Bundle args;

    public EnterExitFragment(Bundle bundle) {
        super();
        args = bundle;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_enter_exit, container, false);

        tvEmail = ((TextView)rootView.findViewById(R.id.textEmail));
        tvFloor1 = ((TextView)rootView.findViewById(R.id.textFloor1));
        tvFloor2 = ((TextView)rootView.findViewById(R.id.textFloor2));
        new FloorFreeSpaces(EnterExitFragment.this).execute();

        btnSend = ((Button)rootView.findViewById(R.id.btnSend));
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Floor(EnterExitFragment.this).execute();
                email = args.getString("email"); //line 55
                tvEmail.setText(email);
            }
        });
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

我看不出你把论点设为碎片。如果你忘记写信了,请通知我

Bundle bundle=new Bundle();
bundle.putString("email", "emasiofnasdbjk");
EnterExitFragment enterExitFragment = new EnterExitFragment();
enterExitFragment.setArguments(bundle); // This line is needed...
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.pager, enterExitFragment);
transaction.addToBackStack(null);
transaction.commit();

请对
enterextfragment.java
文件的第55行进行注释。如果我对第55行进行注释,它将正常工作,但我不会从
MainActiviti.java
获取字符串。请原谅我的文盲,我的意思是在该行之后添加注释,以便我们知道哪一行是第55行:DI对其进行了注释。也许我尝试将字符串从活动发送到片段的方式是错误的。我看不到您将bundle设置为您的片段?在对智能手机上的每个start应用程序进行更改后,我忘记了这一行,buy it仍然没有更改错误,第55行仍然是空指针。LogCat与您发布的日志完全相同?是的,它是相同的。在像我一样创建了选项卡之后,活动和片段之间还有其他通信方式吗?也许替换方法的第一个参数应该不同<代码>事务.replace(R.id.pager,enterextfragment)不,没关系。问题可能是您正在替换containerView,而不是将片段附加/添加到活动中。但是,您实际上可以将
Bundle
属性添加到
enterextfragment
中,并使用EEF构造函数中MainActivity中的
Bundle
变量对其进行初始化。然后引用它,而不是
getArguments()
方法。这当然有效,但如何在
TabsPagerAdapter.java
中将
Bundle
属性添加到
case 0
?我在NitroNbg看到我忘记后添加了这一行,但它不会改变错误。我在下面提到的评论中写到了这一点。你们能在onClick事件之前尝试获取电子邮件,只是为了确保getArgumetns是以null的形式出现在fragment中,还是以null的形式出现在onClick中?
public class EnterExitFragment extends Fragment{
    TextView tvFloor1, tvFloor2, tvEmail;
    Button btnSend;
    Integer floorId, segmentId, spaceId, ticketId;
    String floorName, segmentName, spaceName;
    String email;
    Bundle args;

    public EnterExitFragment(Bundle bundle) {
        super();
        args = bundle;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_enter_exit, container, false);

        tvEmail = ((TextView)rootView.findViewById(R.id.textEmail));
        tvFloor1 = ((TextView)rootView.findViewById(R.id.textFloor1));
        tvFloor2 = ((TextView)rootView.findViewById(R.id.textFloor2));
        new FloorFreeSpaces(EnterExitFragment.this).execute();

        btnSend = ((Button)rootView.findViewById(R.id.btnSend));
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Floor(EnterExitFragment.this).execute();
                email = args.getString("email"); //line 55
                tvEmail.setText(email);
            }
        });
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
Bundle bundle=new Bundle();
bundle.putString("email", "emasiofnasdbjk");
EnterExitFragment enterExitFragment = new EnterExitFragment();
enterExitFragment.setArguments(bundle); // This line is needed...
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.pager, enterExitFragment);
transaction.addToBackStack(null);
transaction.commit();