Android如何调用newInstance构造函数

时间:2014-06-09 10:03:28

标签: android android-viewpager fragment

我想使用ViewPager ImageViewTextview来展示幻灯片。当我调用Fragment.newinstance(int image,String value)构造函数时,它会显示以下错误。

我是否正确调用了构造函数?

错误:

06-09 15:26:17.746: E/AndroidRuntime(14955): FATAL EXCEPTION: main
06-09 15:26:17.746: E/AndroidRuntime(14955): Process: com.example.launchactivity, PID: 14955
06-09 15:26:17.746: E/AndroidRuntime(14955): java.lang.NullPointerException
06-09 15:26:17.746: E/AndroidRuntime(14955):    at com.example.launchactivity.LaunchSliderFragment.newInstance(LaunchSliderFragment.java:33)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at com.example.launchactivity.LaunchActivity$ScreenSlidePagerAdapter.getItem(LaunchActivity.java:44)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.View.measure(View.java:17495)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.View.measure(View.java:17495)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.View.measure(View.java:17495)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.View.measure(View.java:17495)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:412)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.View.measure(View.java:17495)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.View.measure(View.java:17495)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.Choreographer.doCallbacks(Choreographer.java:614)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.Choreographer.doFrame(Choreographer.java:584)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.os.Handler.handleCallback(Handler.java:733)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.os.Looper.loop(Looper.java:146)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at android.app.ActivityThread.main(ActivityThread.java:5602)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at java.lang.reflect.Method.invoke(Method.java:515)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
06-09 15:26:17.746: E/AndroidRuntime(14955):    at dalvik.system.NativeStart.main(Native Method)

活动:

public class LaunchActivity extends FragmentActivity {
    private static final int NUM_PAGES = 4;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    String[] text={"Property Showcase","Bill Payment","Golf Virtual Tour","News and Events"};
    int[] image={R.drawable.slide1,R.drawable.slide2,R.drawable.slide3,R.drawable.slide4};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.launch, menu);
        return true;
    }

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

        @Override
        public Fragment getItem(int position) {
             new LaunchSliderFragment();
            return LaunchSliderFragment.newInstance(image[position], text[position]);
        }

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

}

片段:

public class LaunchSliderFragment extends Fragment {
    private ImageView imageView;
    private TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.launch_slider_fragment, null);
        imageView = (ImageView) view.findViewById(R.id.launch_sliderimage);
        textView = (TextView) view.findViewById(R.id.launch_slidertext);

        return view;
    }

    public static LaunchSliderFragment newInstance(int imageID, String string) {
        LaunchSliderFragment launchSliderFragment = new LaunchSliderFragment();
        Bundle args = new Bundle();
        args.putInt("image", imageID);
        args.putString("text", string);
        launchSliderFragment.setArguments(args);
        launchSliderFragment.imageView.setImageResource(imageID);
        launchSliderFragment.textView.setText(string);
        return launchSliderFragment;

    }

    public LaunchSliderFragment() {
        // TODO Auto-generated constructor stub
    }

}

0 个答案:

没有答案