空指针异常选项卡滑动视图

时间:2014-12-03 11:45:01

标签: java android

我的应用程序在我的片段中包含一个滑动视图。我在其中输入了滑动视图代码的应用程序,显然存在NullPointerException错误,但我无法找到它的确切代码。 有人可以帮忙吗?

这是班级:

 package com.noura.luba;

import java.util.List;
import java.util.Stack;

import android.app.ActionBar;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class Degree_Programs extends FragmentActivity {

    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.degree_programs);

        final ActionBar actionBar = getActionBar();
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        // When swiping between pages, select the
                        // corresponding tab.
                        getActionBar().setSelectedNavigationItem(position);
                    }
                });

        // Specify that tabs should be displayed in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create a tab listener that is called when the user changes tabs.
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            public void onTabUnselected(ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // hide the given tab
            }

            public void onTabReselected(ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // probably ignore this event
            }
        };

        // Add 3 tabs, specifying the tab's text and TabListener
        for (int i = 0; i < 4; i++) {

            if (i == 0)
                actionBar.addTab(actionBar.newTab()
                        .setCustomView(R.layout.study_cycle)
                        .setTabListener(tabListener));
            // .setText("Tab " + (i + 1))

            if (i == 1)
                actionBar.addTab(actionBar.newTab().setCustomView(R.layout.lmd)
                        .setTabListener(tabListener));

            if (i == 2)
                actionBar.addTab(actionBar.newTab()
                        .setCustomView(R.layout.business_administration)
                        .setTabListener(tabListener));
            if (i == 3)
                actionBar.addTab(actionBar.newTab().setText("Tab " + (i + 1))
                        .setTabListener(tabListener));
        }

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection

        switch (item.getItemId()) {
        /*
         * case R.id.settings: Intent intentSettings = new
         * Intent(getApplicationContext(), Settings.class);
         * startActivity(intentSettings); return true;
         */
        case R.id.information:
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.information);
            return true;

        case R.id.logOut:

            Intent intent = new Intent(getApplicationContext(),
                    MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return true;

        case R.id.email:

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("*/*");
            /*
             * i.putExtra(Intent.EXTRA_EMAIL, new String[] {
             * ANDROID_SUPPORT_EMAIL });
             */
            i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
            i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");

            startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public Intent createEmailOnlyChooserIntent(Intent source,
            CharSequence chooserTitle) {
        Stack<Intent> intents = new Stack<Intent>();
        Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
                "noura.h.hadi@gmail.com", null));
        List<ResolveInfo> activities = getPackageManager()
                .queryIntentActivities(i, 0);

        for (ResolveInfo ri : activities) {
            Intent target = new Intent(source);
            target.setPackage(ri.activityInfo.packageName);
            intents.add(target);
        }

        if (!intents.isEmpty()) {
            Intent chooserIntent = Intent.createChooser(intents.remove(0),
                    chooserTitle);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    intents.toArray(new Parcelable[intents.size()]));

            return chooserIntent;
        } else {
            return Intent.createChooser(source, chooserTitle);
        }

    }
}

这是logcat

12-03 02:27:38.216: E/AndroidRuntime(2255): FATAL EXCEPTION: main
12-03 02:27:38.216: E/AndroidRuntime(2255): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noura.luba/com.noura.luba.Degree_Programs}: java.lang.NullPointerException
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.os.Looper.loop(Looper.java:137)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at java.lang.reflect.Method.invoke(Method.java:511)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at dalvik.system.NativeStart.main(Native Method)
12-03 02:27:38.216: E/AndroidRuntime(2255): Caused by: java.lang.NullPointerException
12-03 02:27:38.216: E/AndroidRuntime(2255):     at com.noura.luba.Degree_Programs.onCreate(Degree_Programs.java:30)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.Activity.performCreate(Activity.java:5104)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-03 02:27:38.216: E/AndroidRuntime(2255):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-03 02:27:38.216: E/AndroidRuntime(2255):     ... 11 more

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

这导致问题

mViewPager.setOnPageChangeListener(...);

在调用任何方法之前,您必须先初始化 mViewPager