0

画面の一部をスワイプしようとしています。これにはPageAdapterを使用します。スワイプ部分では、3つのアクティビティがあり
ます1. ShowMap
2. ShowAddress
3. ShowContact

ShowMapアクティビティのレイアウトのIDを取得しようとしていますが、リソースが見つからないという例外が表示されています。これがMyPageAdapterクラス の私のコードです

 public class anyclass extends Activity{
    private class MyPagerAdapter extends PagerAdapter {

        public int getCount(){
            return 3;
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;
            switch (position) {
            case 0: 
                resId = showHotelContact();             
                break;
            case 1:
                resId = showHotelAddress();         
                break;              
            case 2:     
                resId = showHotelMap();             
                break;      
            }

            View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }

    }

    public int showHotelMap()
    {

        Intent bookIntent = new Intent();     
        bookIntent.setClass(HotelMap.this,ShowMap.class);
        startActivityForResult(bookIntent,RESID);

        return RESID;
    }
    public int showHotelAddress()
    {
        int resId;
        resId = R.layout.hoteladdress;
        return resId;
    }
    public int showHotelContact()
    {
        int resId;
        resId = R.layout.hotelcontact;
        return resId;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            RESID = data.getIntExtra("SelectedBook", resultCode);
        }
    }
   }

showMapレイアウトのIDが必要で、PageAdapterに戻そうとしていますが、例外が表示されています。そして、これがShowMapアクティビティのコードです。

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotelmap);

               int  resId = R.layout.hotelmap;
           Intent returnIntent = new Intent();
           returnIntent.putExtra("SelectedBook",resId);
           setResult(RESULT_OK,returnIntent);        
           finish();

        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        MapController mc = mapView.getController();
        double lat = Double.parseDouble("48.85827758964043"); // latitude
        double lon = Double.parseDouble("2.294543981552124"); // longitude
        GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
        mc.animateTo(geoPoint);
        mc.setZoom(15);
        mapView.invalidate();
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);

   }

これが私のLogcatです。

 12-31 13:14:26.740: E/AndroidRuntime(31140): android.content.res.Resources$NotFoundException: Resource ID #0x0
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.content.res.Resources.getValue(Resources.java:1104)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2342)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.content.res.Resources.getLayout(Resources.java:943)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at com..HotelMap$MyPagerAdapter.instantiateItem(HotelMap.java:52)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
    12-31 13:14:26.740: E/AndroidRuntime(31140):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)  
4

2 に答える 2

3
startActivityForResult(bookIntent,RESID);

これは非同期呼び出しだと思います。プログラムは、開始アクティビティが終了して結果を返すまで待機せず、この行を実行して次の行に移動します。そのためRESID、メソッドによって返されるのshowHotelMap()0です。

于 2012-12-31T09:10:54.713 に答える
0

このように試してみてください..

public class TabsViewPagerFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>();
    private PagerAdapter mPagerAdapter;

    private class TabInfo {
         private String tag;
         private Class<?> clss;
         private Bundle args;
         private Fragment fragment;
         TabInfo(String tag, Class<?> clazz, Bundle args) {
             this.tag = tag;
             this.clss = clazz;
             this.args = args;
         }

    }

    class TabFactory implements TabContentFactory {

        private final Context mContext;


        public TabFactory(Context context) {
            mContext = context;
        }

        /** (non-Javadoc)
         * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
         */
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }

    }
    /** (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Inflate the layout
        setContentView(R.layout.tabs_viewpager_layout);
        // Initialise the TabHost
        this.initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
        // Intialise ViewPager
        this.intialiseViewPager();
    }

    /** (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
     */
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }

    /**
     * Initialise ViewPager
     */
    private void intialiseViewPager() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
        //
        this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(this.mPagerAdapter);
        this.mViewPager.setOnPageChangeListener(this);
    }

    /**
     * Initialise the Tab Host
     */
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", Tab1Fragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), ( tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), ( tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        // Default to first tab
        //this.onTabChanged("Tab1");
        //
        mTabHost.setOnTabChangedListener(this);
    }

    /**
     * Add Tab content to the Tabhost
     * @param activity
     * @param tabHost
     * @param tabSpec
     * @param clss
     * @param args
     */
    private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    /** (non-Javadoc)
     * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
     */
    public void onTabChanged(String tag) {
        //TabInfo newTab = this.mapTabInfo.get(tag);
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }

    /* (non-Javadoc)
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
     */
    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
     */
    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        this.mTabHost.setCurrentTab(position);
    }

    /* (non-Javadoc)
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
     */
    @Override
    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub

    }
}

それ以外の場合は、指定されたリンクからヘルプを取得してください..

http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/

于 2013-01-09T07:44:02.263 に答える