0

我在这里研究了各种主题(包括被科学怪人标记为重复的主题),但不知道为什么我会得到一个Null pointer. 我对这一切也很陌生,所以请善待:)

我有一个main_activitybutton单击 a 时在我的数据库上运行一个INSERT命令SQLite并返回id. 然后我将iddown 传递给 myViewPager Activity并将它捆绑起来传递给 initial Summary fragment。当我getArguments()在片段中使用它时,onCreate它会返回一个null pointer. 我在这上面花了几个小时,但不知道为什么它是空的,因为我可以bundles在其他事情上成功通过。

我已经检查了 id 的值 usingSystem.out并且它一直填充到getArguments()

我的Main_Activity那个得到ID:

public class MainActivity extends ListActivity {

…

    private boolean addFactFind(){
        //Get a new sequence number
        newFF();
        //Create new factFind
        System.out.println("THIS IS THE ID in METHOD "+id);
        Intent factFindIntent = new Intent(this, EditFactFind.class);
        factFindIntent.putExtra("id",id);
        System.out.println("THIS IS THE INTENT in METHOD " + factFindIntent);
        //Give fact find new sequence number
        //setSeqOnFrag(newSeqNo);
        //Open new fact find
        startActivity(factFindIntent);
        return true;

    }

    private long newFF() {

        Fact_FindDBHelper dbHelper = new Fact_FindDBHelper(this);
        id = dbHelper.insert();
        // Assign ID to Fact Find
        //EditFactFind.assignFFID(id);
        System.out.println("THIS IS THE FF ID "+id);
        return id;
    }




…

}

我的ViewPager Activity

public class EditFactFind extends FragmentActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks,Summary.TransSummary,Serializable {

    public static final int RESULT_DELETE = -500;
    private boolean isInEditMode = true;
    private boolean isAddingFactFind = true;
    private NavigationDrawerFragment mNavigationDrawerFragment;
    private CharSequence mTitle;
    public String UPDATE_SUMMARY;

    // the pager widget, handles swipes
    private ViewPager mViewPager;
    // the pager adapter, which provides the pages to the view pager widget
    private PagerAdapter mPagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_factfind);
        // Locate the viewpager in edit_factfind.xml
       // ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        // Set the ViewPagerAdapter into ViewPager
       // viewPager.setAdapter(new Pager(getSupportFragmentManager()));

        final EditText titleEditText = (EditText)findViewById(R.id.titleEditText);
        final TextView dateTextView = (TextView)findViewById(R.id.dateTextView);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer); // Activity_nav_drawer.xml
        mTitle = getTitle();

       //Get the ID passed down from the Main_Activity
        Intent mIntent = getIntent();
        long ID = mIntent.getLongExtra("id", 0);
        System.out.println("THIS IS THE ID at EDIT " + ID);

        //Create a bundle to pass the id to the fragment
        Summary sumobj = new Summary();
        Bundle bundle = new Bundle();
        bundle.putLong("id",ID);
        System.out.println(bundle);

        sumobj.setArguments(bundle);


        //Instantiate a ViewPager and a PagerAdapter
        mViewPager=(ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new Pager(getSupportFragmentManager(),bundle);
        mViewPager.setAdapter(mPagerAdapter);

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));


        System.out.println(mViewPager);
        System.out.println(mPagerAdapter);



   //     Summary sumFragment = (Summary) mPagerAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem());

   //     System.out.println(sumFragment);
   //     sumFragment.updateFFID(ID);

     }
…
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

…



}

最后是我的Summary Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = this.getArguments();
    long id = args.getLong("id",0); // This is where the Null Pointer crops up
    TextView FFID = (TextView) getView().findViewById(R.id.Sum_FFID);
    FFID.setText(String.valueOf(id));
}'

寻呼机类

public class Pager extends FragmentStatePagerAdapter {
    public Bundle fragmentBundle;

    public Pager(android.support.v4.app.FragmentManager fm, Bundle data) {

        super(fm);
        fragmentBundle = data;
    }
    // number of pages to show in slider
    public static final int NUM_PAGES = 28;

    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    // Tab Titles
    private String tabtitles[] = new String[] { "Summary",
            "Personal",
            …….
            "Checklist"};
    Context context;


    @Override
    public Fragment getItem(int position){

        Fragment fragment = new Summary();

        switch(position) {
            case 0:
                return fragment = new Summary();
            case 1:
                return fragment = new Personal();
   ………
        }

        fragment.setArguments(this.fragmentBundle);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return tabtitles[position];
    }

    public Fragment getRegisteredFragment(int position){
        return registeredFragments.get(position);
    }
}
4

0 回答 0