2013-10-13 87 views
0

的碰撞這是我的菜單的Java代碼,其包括在一個滑動視圖界面片段,其中i有單獨的活動的每個片段。我遇到一個應用程序類,通過調用這個活動,我希望有人可以幫助我解決我的問題,因爲我是一名Android編程初學者。非常感謝您的幫助。的Android:片段代碼使得應用

package com.example.lazynumber; 

import java.util.Locale; 

import android.annotation.SuppressLint; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.NavUtils; 
import android.support.v4.view.ViewPager; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainMenu extends FragmentActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
* will keep every loaded fragment in memory. If this becomes too memory 
* intensive, it may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
ViewPager mViewPager; 

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

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the app. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(
      getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 


} 

@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, menu); 
    return true; 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a DummySectionFragment (defined as a static inner class 
     // below) with the page number as its lone argument. 
     switch(position){ 
     case 0: 
      Fragment fragment = new GetNumberFragment(); 
      return fragment; 

     case 1: 
      Fragment fragment2 = new QRFragment(); 
      return fragment2; 

     case 2: 
      Fragment fragment3 = new ProfileFragment(); 
      return fragment3; 

     default: 
      return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 
      return getString(R.string.title_section1).toUpperCase(l); 
     case 1: 
      return getString(R.string.title_section2).toUpperCase(l); 
     case 2: 
      return getString(R.string.title_section3).toUpperCase(l); 
     } 
     return null; 
    } 
} 

/** 
* A dummy fragment representing a section of the app, but that simply 
* displays dummy text. 
*/ 
public static class GetNumberFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public GetNumberFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView =  inflater.inflate(R.layout.activity_get_number_intro, 
       container, false); 
     return rootView; 
    } 
} 


@SuppressLint("ValidFragment") 
public class QRFragment extends Fragment{ 

    public QRFragment(){}; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     View rootView2 = inflater.inflate(R.layout.qrintro, container, false); 

     Button testingButton = (Button)findViewById(R.id.goinQRPay); 
     testingButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       goTesting(); 
      } 
     }); 
     return rootView2; 
    } 
} 

private void goTesting(){ 
    Intent goTesting = new Intent(this, Testingbutton.class); 
    startActivity(goTesting); 
} 

public static class ProfileFragment extends Fragment{ 

    public ProfileFragment(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     View rootView3 = inflater.inflate(R.layout.activity_profile_setting, container, false); 
     return rootView3; 
    } 
} 
} 

這些是我的xml代碼以供參考,如果需要的話。 第一個是MainMenu的XML

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/pager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainMenu" > 

<!-- 
This title strip will display the currently visible page title, as well as the page 
titles for adjacent pages. 
--> 

<android.support.v4.view.PagerTitleStrip 
    android:id="@+id/pager_title_strip" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top" 
    android:background="#33b5e5" 
    android:paddingBottom="4dp" 
    android:paddingTop="4dp" 
    android:textColor="#fff" /> 

</android.support.v4.view.ViewPager> 

這下一個XML是我需要具有這樣的功能,其是QRFragment活性XML接口的一個。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".QRIntro" > 

<ImageView 
    android:id="@+id/qrintropic" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/qrlazy" /> 

<Button 
    android:id="@+id/goinQRPay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/qrintropic" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="20dp" 
    android:text="@string/buttontoqr" />" 

</RelativeLayout> 

回答

0

這些片段需要插入某種容器佈局,如FrameLayout。如果你添加,那麼你可以使用這種方法來代替片段

public static void replaceFragment(Activity activity, int containerlayoutId, Fragment newFragment) { 
    FragmentTransaction transaction = activity.getFragmentManager().beginTransaction().replace(containerlayoutId, newFragment); 
    transaction.commit(); 
    activity.getFragmentManager().executePendingTransactions();  
} 
+0

感謝您的答覆。但我應該在哪裏放這個代碼? –