it-swarm.com.de

Versuchen Sie, die virtuelle Methode 'void Android.support.v4.view.ViewPager.setAdapter (Android.support.v4.view.PagerAdapter)' für eine Nullobjektreferenz aufzurufen

Ich versuche, das Slide-Tab-Layout mit Android-Materialdesign zu implementieren. Aber es gibt mir NullPointerException. Hier ist mein Code bisher:

MainActivity.Java

public class MainActivity extends ActionBarActivity {
    private Toolbar toolbar;
    private ViewPager pager;
    private SlidingTabLayout nLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_appbar);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_fragment);
        drawerFragment.setUp(R.id.navigation_drawer_fragment, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        nLayout = (SlidingTabLayout) findViewById(R.id.tabs);
        nLayout.setViewPager(pager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.navigate) {
            startActivity(new Intent(this, SubActivity.class));

        }

        return super.onOptionsItemSelected(item);
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        String tabs[];

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            tabs = getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int position) {
            MyFragment myFragment = MyFragment.getInstance(position);
            return myFragment;
        }

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

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

    public static class MyFragment extends Fragment {
        private TextView textView;

        public static MyFragment getInstance(int position) {
            MyFragment myFragment = new MyFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            myFragment.setArguments(args);
            return myFragment;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_my, container, false);
            textView = (TextView) layout.findViewById(R.id.position);
            Bundle bundle = getArguments();
            if (bundle != null) {
                textView.setText("The page selected is " + bundle.getInt("position"));
            }
            return layout;
        }
    }


}

activity_main.xml

<LinearLayout
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical"
    tools:context="carsaleapp.shanaka.com.carsaleapp.MainActivity">

    <include
        Android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <app.shanaka.tabs.SlidingTabLayout
        Android:id="@+id/tabs"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content" />


    <Android.support.v4.view.ViewPager
        Android:id="@+id/pager"
        Android:layout_width="match_parent"
        Android:layout_height="0dp"
        Android:layout_weight="1" />


</LinearLayout>

Dies ist der Fehler, den ich nach dem Ausführen meiner Anwendung bekam:

Java.lang.RuntimeException: Unable to start activity ComponentInfo{carsaleapp.shanaka.com.carsaleapp/carsaleapp.shanaka.com.carsaleapp.MainActivity}: Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v4.view.ViewPager.setAdapter(Android.support.v4.view.PagerAdapter)' on a null object reference
            at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2298)
            at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
            at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
            at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
            at Android.os.Handler.dispatchMessage(Handler.Java:102)
            at Android.os.Looper.loop(Looper.Java:135)
            at Android.app.ActivityThread.main(ActivityThread.Java:5221)
            at Java.lang.reflect.Method.invoke(Native Method)
            at Java.lang.reflect.Method.invoke(Method.Java:372)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:899)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
     Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v4.view.ViewPager.setAdapter(Android.support.v4.view.PagerAdapter)' on a null object reference
            at carsaleapp.shanaka.com.carsaleapp.MainActivity.onCreate(MainActivity.Java:42)
            at Android.app.Activity.performCreate(Activity.Java:5933)
            at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1105)
            at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2251)
            at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
            at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
            at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
            at Android.os.Handler.dispatchMessage(Handler.Java:102)
            at Android.os.Looper.loop(Looper.Java:135)
            at Android.app.ActivityThread.main(ActivityThread.Java:5221)
            at Java.lang.reflect.Method.invoke(Native Method)
            at Java.lang.reflect.Method.invoke(Method.Java:372)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:899)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)

Sie sagen immer wieder, dass das Objekt pager null ist. Aber für mich habe ich es richtig eingestellt. 

pager = (ViewPager) findViewById(R.id.pager);
11
PereraSH

Sie haben Ihren View-Pager in activity_main.xml, Während Ihr Layout für die Aktivität setContentView(R.layout.activity_main_appbar) lautet. Sie haben keinen View-Pager in activity_main_appbar.xml.

findViewById sucht nach einer Ansicht in der aktuellen Ansichtshierarchie. Ich denke, es sollte setContentView(R.layout.activity_main); sein, aber überprüfen Sie, welche Ansichten Sie im enthaltenen Layout haben app_bar.xml

25
Raghunandan

Möglicherweise übergeben Sie eine falsche ID von viewPager. Bitte überprüfen Sie Ihre ID aus der XML-Datei

1
Rishabh Saxena