2013-10-13 5 views
0

이것은 각 조각에 대해 별도의 작업을 수행 한 스 와이프보기 인터페이스에서 조각을 포함하는 메뉴 자바 코드입니다. 이 액티비티를 호출하여 애플리케이션 클래스를 만난다. 나는 안드로이드 프로그래밍의 초보자로서 누군가 내 문제를 해결하는 데 도움이되기를 바란다. 귀하의 도움은 매우 감사하겠습니다.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 코드입니다. 제 하나는 XML을 MAINMENU

<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

답장을 보내 주셔서 감사합니다. 하지만이 코드를 어디에 넣어야합니까? –

관련 문제