如何在滑动选项卡中移动带有数据的屏幕?

时间:2014-03-05 02:53:48

标签: android

我的需求是当我点击Tab1fragment中的按钮时,屏幕移动到下一个屏幕(Tab2Fragment)以及我在edittext中写的数据。

这是我的代码,但没有用,有人请帮助我。

Tab1Fragment.java

package com.example.tabswithswipe;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;


    public class Tab1Fragment extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.tab1, container, false);
            Button btSend = (Button)rootView.findViewById(R.id.button1);
            final EditText editSendata =     (EditText)rootView.findViewById(R.id.editText1);

            btSend.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Tab2Fragment fragment = new Tab2Fragment();
                        Bundle args = new Bundle();
                        args.putString("data",editSendata.getText().toString());
                        fragment.setArguments(args);
                    }
            });
            return rootView;
        }
    }

tab1.xml

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tab1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="31dp"
                android:ems="10" />


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fragment1"
            android:layout_marginLeft="29dp"
            android:layout_marginTop="22dp"
            android:layout_toRightOf="@+id/textView1"
            android:text="Button" />

    </RelativeLayout>

Tab2Fragment.java

package com.example.tabswithswipe;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Tab2Fragment extends Fragment{
     private TextView txtResultData;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tab2, container, false);
        this.txtResultData = (TextView)rootView.findViewById(R.id.textView1);

        String i = getArguments().getString("data");
        txtResultData.setText(i);

        return rootView;
    }
}

tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="63dp"
        android:layout_marginTop="23dp"
        android:text="TextView" />

</RelativeLayout>

MainActivity.java

package com.example.tabswithswipe;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;



public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
     private ViewPager viewPager;
        private TabsPagerAdapter mAdapter;
        private ActionBar actionBar;
        // Tab titles
        private String[] tabs = { "Tab1", "Tab2", "Tab3" };

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

            // Initilization
            viewPager = (ViewPager) findViewById(R.id.pager);
            actionBar = getActionBar();
            mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

            viewPager.setAdapter(mAdapter);
            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // Adding Tabs
            for (String tab_name : tabs) {
                actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(this));
            }

            /**
             * on swiping the viewpager make respective tab selected
             * */
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageSelected(int position) {
                    // on changing the page
                    // make respected tab selected
                    actionBar.setSelectedNavigationItem(position);
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                }
            });
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // on tab selected
            // show respected fragment view
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

TabsPagerAdapter.java

package com.example.tabswithswipe;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.tabswithswipe.Tab1Fragment;
import com.example.tabswithswipe.Tab2Fragment;
import com.example.tabswithswipe.Tab3Fragment;

public class TabsPagerAdapter extends FragmentPagerAdapter{
    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Tab1Fragment activity
            return new Tab1Fragment();
        case 1:
            // Tab2Fragment activity
            return new Tab2Fragment();
        case 2:
            // Tab3Fragment activity
            return new Tab3Fragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}
Log中的

错误

03-06 03:17:00.740: E/AndroidRuntime(1375): FATAL EXCEPTION: main
03-06 03:17:00.740: E/AndroidRuntime(1375): Process: com.example.tabswithswipe, PID: 1375
03-06 03:17:00.740: E/AndroidRuntime(1375): java.lang.NullPointerException
03-06 03:17:00.740: E/AndroidRuntime(1375):     at com.example.tabswithswipe.Tab2Fragment.onCreateView(Tab2Fragment.java:20)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.View.measure(View.java:16458)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.View.measure(View.java:16458)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.View.measure(View.java:16458)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.View.measure(View.java:16458)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.os.Handler.handleCallback(Handler.java:733)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.os.Looper.loop(Looper.java:137)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at java.lang.reflect.Method.invoke(Method.java:515)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-06 03:17:00.740: E/AndroidRuntime(1375):     at dalvik.system.NativeStart.main(Native Method)
03-06 03:17:40.870: I/Process(1375): Sending signal. PID: 1375 SIG: 9

2 个答案:

答案 0 :(得分:0)

如果在您使用的Tab2Fragment中会发生什么:

String i = getArguments().getString("key");

答案 1 :(得分:0)

您的寻呼机适配器会创建标签所需的所有三个Fragment。所以你在onClick监听器中设置参数的方式是错误的。

试试这个:在Tab1Fragment中声明一个界面并在MainActivity中实现该界面,以便回调方法从Tab1Fragment获取所需的数据。如何记录here

要滚动到第二个标签页,您可能需要在视图寻呼机的适配器上调用setCurrentItem()方法。

通过调用Tab2Fragment实例上的findFragmentById来获取对FragmanetManager的引用。要做到这一点,首先必须在片段上设置标记,然后在适配器的getItem方法中返回它。引用片段后,您可以调用方法并发送片段可以使用的数据来更新其视图。