ViewPager の DetailFragmentB の DetailFragmentA から TextView を変更しようとしています。それに続いて、私はそれをやろうとしました。しかし、ボタンを押して FragmentB の TextView を変更すると、次の 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 を変更すると、次の 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();
今、私は NullPointerException を取得します:
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)