0

我得到這個error當我嘗試創建一個ViewPagerActivity採用Android工作室Viewpager實施失敗

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

活性這裏:

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

錯誤在這裏發生在碎片ENT在線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),其行爲如下所述。我從來沒有看到Home是'title_section1' – vamsiampolu

相關問題