应用程序在启动ActionBarActivity时崩溃

时间:2013-10-30 16:37:52

标签: android intellij-idea android-actionbar android-actionbaractivity

我想开始这项活动

mport android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import ua.khuta.citycard.R;
import ua.khuta.citycard.adapters.TabsPagerAdapter;
public class MainActivity extends ActionBarActivity implements
    ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Top Rated", "Games", "Movies"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getSupportActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
        //   initialization();
    }
}


@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    //To change body of implemented methods use File | Settings | File Templates.
}
}

代码:intent = new Intent(this, MainActivity.class); startActivity(intent);

首先我得到错误的主题myActivity主题,我把它改为

<activity android:name=".ui.MainActivity"
            android:theme="@style/Theme.AppCompat"/>

错误日志是:

10-30 18:36:41.928: ERROR/AndroidRuntime(23156): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在我实例化新标签页时,向我看来您的标签适配器中存在问题。

在发布适配器代码和所有片段之前无法确定。