0

我似乎在我的 Android 应用程序中破坏了某些东西,但我无法弄清楚它是什么......我对此很陌生,这可能很简单。

10-26 00:12:12.182    2928-2928/com.myapp.application E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.application/com.myapp.application.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.myapp.application.MainActivity.onTabSelected(MainActivity.java:94)
            at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
            at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
            at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
            at com.myapp.application.MainActivity.onCreate(MainActivity.java:45)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

代码: http: //pastebin.com/9R5WM5M9

4

2 回答 2

0

空指针似乎在onTabSelected事件处理程序内部,很可能是因为mViewPager在第一个 onTabSelected 事件时为空。试试这个:

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    int tabPosition = tab.GetPosition();

    if (null != mViewPager)
    {
        String newTitle;
        mViewPager.setCurrentItem(tabPosition);

        switch (tabPosition)
        {
            case 0:
                newTitle = getString(R.string.action_title_timeline);
                break;
            case 1:
                newTitle = getString(R.string.action_title_mentions);
                break;
            case 2:
                newTitle = getString(R.string.action_title_inbox);
                break;
            case 3:
                newTitle = getString(R.string.action_title_search);
                break;
            case 4:
                newTitle = getString(R.string.action_title_profile);
                break;
        }

        if  (null != newTitle && !newTitle.isEmpty())
        {
            setTitle(newTitle);
        }
    }
}
于 2013-10-26T01:18:46.420 回答
0

是的,它可能是空的 ViewPager。在添加选项卡之前尝试获取参考。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_inbox).setTabListener(this));
   ...
于 2013-10-26T01:25:46.393 回答