1

我想开始这个活动

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);

首先我得到错误 abaut 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)
4

1 回答 1

0

在我看来,当实例化新选项卡时,您的选项卡适配器有问题。

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

于 2013-12-14T13:41:57.240 回答