2016-01-09 156 views
1

我跟進android viewpager tutorial,而我得到這個錯誤viewpager片段返回空值

顯示java.lang.NullPointerException:試圖調用虛擬方法無效 android.widget.TextView.setText(java中。在空 對象引用

我知道它說,我設置爲空值lang.CharSequence)」,但我的qeustion爲什麼它可以爲空?我返回值

public class SingleViewActivity extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_page_view); 
     DemoCollectionPagerAdapter 
     mDemoCollectionPagerAdapter = 
       new DemoCollectionPagerAdapter(
         getSupportFragmentManager()); 
     ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mDemoCollectionPagerAdapter); 
    } 

    /* if (savedInstanceState == null) { 
      // During initial setup, plug in the details fragment. 
      MyFragment myrag = new MyFragment(); 
      myrag.setArguments(getIntent().getExtras()); 
      getSupportFragmentManager().beginTransaction().add(android.R.id.content, myrag).commit(); 
     } 
*/ 
     // Since this is an object collection, use a FragmentStatePagerAdapter, 
     // and NOT a FragmentPagerAdapter. 

     public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter { 
      public DemoCollectionPagerAdapter(FragmentManager fm) { 
       super(fm); 
      } 

      @Override 
      public Fragment getItem(int i) { 
       Fragment fragment = new DemoObjectFragment(); 
       Bundle args = new Bundle(); 
       // Our object is just an integer :-P 
       args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); 
       fragment.setArguments(args); 
       return fragment; 
      } 

      @Override 
      public int getCount() { 
       return 100; 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 
       return "OBJECT " + (position + 1); 
      } 
     } 

// Instances of this class are fragments representing a single 
// object in our collection. 
     public static class DemoObjectFragment extends Fragment { 
      public static final String ARG_OBJECT = "object"; 

      @Override 
      public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) { 
       // The last two arguments ensure LayoutParams are inflated 
       // properly. 
       View rootView = inflater.inflate(
         R.layout.fragment_collection_object, container, false); 
       Bundle args = getArguments(); 
       ((TextView) rootView.findViewById(android.R.id.text1)).setText(Integer.toString(args.getInt(ARG_OBJECT))); 
       return rootView; 
      } 
     } 
    } 

回答

1

變化

((TextView) rootView.findViewById(android.R.id.text1)).setText(...) 

((TextView) rootView.findViewById(R.id.text1)).setText(...) 

其中@+id/text1是你的TextView的id。

+0

是的你是對的,它在android文件中的錯誤,謝謝 – Moudiz

+0

不客氣@Moudiz –

1

你必須使用View.findViewById(R.id.the_id_of_the_view)去TextView的一個參考,你可以在它上面調用TextView.setText()之前。

注意:我在findViewById()方法中刪除了'android'。

+0

我添加文本的view.id((TextView)rootView.findViewById(android.R.id.text1))..我應該添加什麼? – Moudiz

+0

刪除android.R.id –