从ViewPager的FragmentS更改FragmentS的内容

时间:2013-04-14 16:29:27

标签: android android-fragments android-viewpager

我尝试从ViewPager中更改DetailFragmentB中DetailFragmentA的TextView。接下来是我试图做的代码。但是,如果按下按钮更改FragmentB中的TextView,则App会跟随LogCat崩溃。我能做什么?它有哪些静态变量,但这不是一个真正好的解决方案。

这是我的活动和适配器:

public class ViewPagerExample extends FragmentActivity {
    private MyAdapter mAdapter;
    private ViewPager mPager;

    public Fragment detailFragmentA = new Fragment();
    public Fragment detailFragmentB = new Fragment();


    public static String saveText;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mAdapter = new MyAdapter(getApplicationContext(), getSupportFragmentManager(), detailFragmentA, detailFragmentB);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
    }

    public  void refresh(String text){
        ((DetailFragmentB) detailFragmentB).changeText(text);   
    }

    public static class MyAdapter extends FragmentPagerAdapter {
        private Context _context;
        private Fragment fragmentA;
        private Fragment fragmentB;


        public MyAdapter(Context context, FragmentManager fm, Fragment fragA, Fragment fragB) {
            super(fm);  
            _context=context;
            fragmentA = fragA;
            fragmentB = fragB;

            }
        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
            case 0:
                fragmentA =DetailFragmentA.newInstance(_context);   
                return fragmentA;
            case 1:
                fragmentB=DetailFragmentB.newInstance(_context);    
                return fragmentB;
            }
            return null;
        }
    }

}

这是FragmentA类:

public class DetailFragmentA extends Fragment 
                                    implements OnClickListener{

    Button button;
    TextView textB;
    EditText edit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

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

    }

    public static Fragment newInstance(Context context) {
        DetailFragmentA f = new DetailFragmentA();  

        return f;
    }

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

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.details, null);
        TextView textView = (TextView) root.findViewById(R.id.detailsText);
        textView.setText("Testing");
        button = (Button)root.findViewById(R.id.button1);
        button.setOnClickListener(this);      

        edit = (EditText) root.findViewById(R.id.editText1);

     return root;
    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Log.e("Test", "onClick");

        ((ViewPagerExample) getActivity()).refresh(edit.getText().toString()); 

    }

这就是FragmentA:

public class DetailFragmentB extends Fragment {

    TextView textView;
     String saveText = "default";

    public static Fragment newInstance(Context context) {
        DetailFragmentB f = new DetailFragmentB();  

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.details_2, container, false);
        textView = (TextView) view.findViewById(R.id.detailsTextB);
        textView.setText(saveText);
        return view;
    }

    public void changeText(String editable){

        saveText = editable;
        textView.setText(saveText);
    }
}

但如果按下按钮更改FragmentB中的TextView,则App会跟随LogCat崩溃。我该怎么办?

04-14 17:46:24.343: E/AndroidRuntime(5667): FATAL EXCEPTION: main
04-14 17:46:24.343: E/AndroidRuntime(5667): java.lang.ClassCastException: android.support.v4.app.Fragment
04-14 17:46:24.343: E/AndroidRuntime(5667):     at com.example.viewpagerexample.ViewPagerExample.refresh(ViewPagerExample.java:35)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at com.example.viewpagerexample.DetailFragmentA.onClick(DetailFragmentA.java:64)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at android.view.View.performClick(View.java:2408)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at android.view.View$PerformClick.run(View.java:8817)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at android.os.Handler.handleCallback(Handler.java:587)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at android.os.Looper.loop(Looper.java:144)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at java.lang.reflect.Method.invoke(Method.java:521)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-14 17:46:24.343: E/AndroidRuntime(5667):     at dalvik.system.NativeStart.main(Native Method)

修改 我在ViewPagerExample类中改为跟随:

public Fragment detailFragmentA = new DetailFragmentA();
public Fragment detailFragmentB = new DetailFragmentB();

现在我得到一个NullPointerExeception:

04-14 19:50:54.673: E/AndroidRuntime(5917): FATAL EXCEPTION: main
04-14 19:50:54.673: E/AndroidRuntime(5917): java.lang.NullPointerException
04-14 19:50:54.673: E/AndroidRuntime(5917):     at com.example.viewpagerexample.DetailFragmentB.changeText(DetailFragmentB.java:49)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at com.example.viewpagerexample.ViewPagerExample.refresh(ViewPagerExample.java:35)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at com.example.viewpagerexample.DetailFragmentA.onClick(DetailFragmentA.java:64)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at android.view.View.performClick(View.java:2408)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at android.view.View$PerformClick.run(View.java:8817)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at android.os.Handler.handleCallback(Handler.java:587)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at android.os.Looper.loop(Looper.java:144)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at java.lang.reflect.Method.invoke(Method.java:521)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-14 19:50:54.673: E/AndroidRuntime(5917):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您正在尝试将fragmentB转换为detailFragmentB。但它可能不是detailFragmentB,您最初将其设置为等于新的Fragment()。将其更改为新的DetailFragmentB。对A做同样的事情,因为我确定你的错误也一样。