0

내가이 error을 얻을 실패 ViewPagerActivity에 대한 :Viewpager 구현 내가 시도하고 만들 때

public class PlaceHolderFragment extends Fragment { 

private static final String ARG_SECTION_NUMBER = "section_number"; 
TextView text_header,text_content; 


@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState) 
{ 
    super.onCreateView(inflater, parent, savedInstanceState); 
    View v=inflater.inflate(R.layout.layout_slide_page,parent,false); 
    text_header=(TextView)v.findViewById(R.id.section_label); 
    text_content=(TextView)v.findViewById(R.id.section_content); 
    return v; 
} 

public PlaceHolderFragment() 
{ 
    super(); 
} 

public static PlaceHolderFragment newInstance(int sectionNumber) 
{ 
    PlaceHolderFragment fragment = new PlaceHolderFragment(); 
    switch(sectionNumber) 
    { 
     case 0: 
      fragment.text_header.setText(R.string.title_section1); 
      fragment.text_content.setText(R.string.content_section1); 
      break; 
     case 1: 
      fragment.text_header.setText(R.string.title_section2); 
      fragment.text_content.setText(R.string.content_section2); 
      break; 
     case 2: 
      fragment.text_header.setText(R.string.title_section3); 
      fragment.text_content.setText(R.string.content_section3); 
      break; 
    } 
    return fragment; 
} 
} 

활동은 다음과 같습니다

FATAL EXCEPTION: main 
java.lang.NullPointerException 
at com.blutech.viewpager.PlaceHolderFragment.newInstance(PlaceHolderFragment.java:44) 
at com.blutech.viewpager.LayoutSlideActivity$SectionsPagerAdapter.getItem(LayoutSlideActivity.java:94) 
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97) 
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:982) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 
at android.view.View.measure(View.java:15848) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:15848) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) 
at android.view.View.measure(View.java:15848) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189) 
at android.view.View.measure(View.java:15848) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
at android.view.Choreographer.doFrame(Choreographer.java:532) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(NativeMethod) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

이 내가 사용한 조각입니다 여기에 :

public class LayoutSlideActivity extends ActionBarActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link 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.layout_slide); 



    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    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.layout_slide, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 





/** 
* 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 PlaceholderFragment (defined as a static inner class below). 
     return PlaceHolderFragment.newInstance(position); 
    } 

    @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; 
    } 
} 
} 

여기서 오류는 fragm에서 비롯된 것입니다. 라인 (44)에 이비인후과 :

fragment.text_header.setText(R.string.title_section2); 

다음 에러 라인은 FragmentPagerAdaptergetItem(int position) 방법이다.

return PlaceHolderFragment.newInstance(position); 

답변

1

스위치 케이스를 onCreateView으로 옮기고 newInstance에서 섹션 번호를 설정하십시오. 예 :

public class PlaceHolderFragment extends Fragment { 

private static final String ARG_SECTION_NUMBER = "section_number"; 
TextView text_header,text_content; 
int sectionNumber; 


@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState) 
{ 
    super.onCreateView(inflater, parent, savedInstanceState); 
    View v=inflater.inflate(R.layout.layout_slide_page,parent,false); 
    text_header=(TextView)v.findViewById(R.id.section_label); 
    text_content=(TextView)v.findViewById(R.id.section_content); 
    switch(sectionNumber) 
    { 
     case 0: 
      text_header.setText(R.string.title_section1); 
      text_content.setText(R.string.content_section1); 
      break; 
     case 1: 
      text_header.setText(R.string.title_section2); 
      text_content.setText(R.string.content_section2); 
      break; 
     case 2: 
      text_header.setText(R.string.title_section3); 
      text_content.setText(R.string.content_section3); 
      break; 
    } 
    return v; 
} 

public PlaceHolderFragment() 
{ 
    super(); 
} 

public static PlaceHolderFragment newInstance(int sectionNumber) 
{ 
    PlaceHolderFragment fragment = new PlaceHolderFragment(); 
    fragment.setSectionNumber(sectionNumber); 
    return fragment; 
} 
public void setSectionNumber(int sectionNumber){ 
    this.sectionNumber=sectionNumber; 
} 
} 

문제는 뷰를 생성의 시간 newInstance에있다가 호출되지 않습니다 그래서 text_header 및 TEXT_CONTENT는 초기화되지 않습니다.

+0

ViewPager는 작동하지만 newInstance와 getItem이 위치가 이상한 일을하고있는 것 같습니다. – vamsiampolu

+0

무슨 일이 일어나고 있습니까? –

+0

다음은 [내가 사용하는 문자열] (http://pastebin.com/G7x9axL1)이며 동작은 아래에 설명되어 있습니다. 'title_section1' 인 홈이 보이지 않습니다 – vamsiampolu