DialogでViewpagerを使おうとした時のお話。
どうもAlertDialog.Builderを今まで使ってたのだが、とうとう不具合?が出てしまった。
今回DialogでViewpagerを使うために他のやつのように
// カスタムビューを設定 LayoutInflater inflater = LayoutInflater.from(this); final View layout = inflater.inflate(R.layout.dialog_kusuri, (ViewGroup) findViewById(R.id.layout_root));
のDialogのlayoutからlayout.findViewById(R.id.pager)をViewPagerにしたのだが、何故かViewが見つからなくて落ちてしまう。(java.lang.IllegalStateException: Fragment does not have a view)
頑張ってpagerを見つけようとしたけどダメだった。
これは何でだろーってしらべた結果どうもAlertDialog.Builderを使ってるのが悪かったらしい
これからはDialogFragmentを使えとのこと。
ためしにDialogFragmentを使うとすんなり動いた。
PageDialog.java
package com.medicalmonitor; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; public class PageDialog extends DialogFragment { private final static String KEY_URL = "diaog_key_urls"; public static PageDialog newInstance(String[] urls) { PageDialog fragment = new PageDialog(); Bundle args = new Bundle(); args.putStringArray(KEY_URL, urls); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.dialog_kusuri, container, false); String[] urls = getArguments().getStringArray(KEY_URL); final ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager2); final Dialog_Fragment_pager adapter = new Dialog_Fragment_pager(getChildFragmentManager()); viewPager.setAdapter(adapter); getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().setCanceledOnTouchOutside(true); return view; } }
Dialog_Fragment_pager.java
package com.medicalmonitor; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.view.ViewGroup; import java.util.HashMap; import java.util.Map; public class Dialog_Fragment_pager extends FragmentPagerAdapter {//FragmentStatePagerAdapterはメモリ破棄する、保存するならこっち private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; public Dialog_Fragment_pager(FragmentManager fm) { super(fm); mFragmentManager = fm; mFragmentTags = new HashMap<Integer, String>(); } private static final String ARG_SECTION_NUMBER = "section_number"; @Override public Fragment getItem(int position) { return frag_dialog_pager_main.newInstance(position); } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { return "Page " + position; } @Override public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { // record the fragment tag here. Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } }
frag_dialog_pager_main.java
package com.medicalmonitor; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.Loader; import android.text.TextUtils; import android.text.format.Time; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import org.json.JSONObject; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class frag_dialog_pager_main extends Fragment { private static final String KEY_URL_STR = "urlStr"; private static final int KANJANUM = 0; private static final int UNFINISHEDNUM = 1; private static final int FINISHEDNUM = 2; float juuryou=0; int mNum;//画面の番号1はunfinished2はfinishedとか final List<CustomData> localobjects = new ArrayList<CustomData>();//ローカルオブジェクト static frag_dialog_pager_main newInstance(int num) { frag_dialog_pager_main f = new frag_dialog_pager_main(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } /** * When creating, retrieve this instance's number from its arguments. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } AlertDialog.Builder alertDlg2; private AlertDialog m_dlg; View rootView; ListView listView; //グローバル変数 Globals globals; ArrayAdapter adapter; Fragment_pick pick = new Fragment_pick(); // MainActivity activity = (MainActivity) getActivity(); MainActivity main = new MainActivity(); @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { globals = (Globals) getActivity().getApplication(); getActivity().getApplicationContext(); return inflater.inflate(R.layout.frag_picking_finished, container, false); } @Override public void onStop() { //ImageCache.deleteAll(getCacheDir()); super.onStop(); } @Override public void onStart() { super.onStart(); } }
↑無駄な処理多いなー
ダイアログ使うときは
PageDialog newFragment = PageDialog.newInstance(null); newFragment.show(getSupportFragmentManager(), TAG);
だけ!
まぁ動いてよかったー