在片段中的oncreateView()中获取nullpointerException

时间:2014-04-03 12:07:51

标签: android android-fragments nullpointerexception android-actionbar

此处我是FragmentActionBar的新用户。

我正在使用ActionBar到我的Fragment类。但是我在这行中得到NullPointerException

我的Android版本也是Google API [Android 4.0]

 actionBar.setDisplayHomeAsUpEnabled(true);

我正在使用此API

public class TodaysDealFragment extends Fragment implements
ActionBar.TabListener{

private View myview = null;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Mumbai", "Shopping", "Travel"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    myview = inflater.inflate(R.layout.activity_main, container, false);
    // Initilization
    viewPager = (ViewPager) myview.findViewById(R.id.pager);
    actionBar = getActivity().getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    mAdapter = new TabsPagerAdapter(getFragmentManager());

    viewPager.setAdapter(mAdapter);
     System.out.println("amar actionBar is==="+actionBar);
       //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));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });



return myview;  
}


@Override
public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    viewPager.setCurrentItem(arg0.getPosition());
}

@Override
public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}



  }

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.test"
   android:versionCode="12"
   android:versionName="2.4" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:name="com.test.TestApplication"
    android:allowClearUserData="true"
    android:enabled="true"
    android:icon="@drawable/ic_launcher"
    android:hardwareAccelerated="true"
    android:theme="@android:style/Theme.NoTitleBar"
    android:label="@string/app_name"
    android:largeHeap="true"
    >

    <activity
        android:name="com.test.TestActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateAlwaysHidden" 
        android:launchMode="singleTop">
    </activity>
    <activity
        android:name="com.test.ui.LoginActivity"
        android:screenOrientation="portrait"
       >
    </activity>
    <activity
        android:name="com.test.ui.DailyEmailActivity"
        android:screenOrientation="portrait" />
    <activity
        android:name="com.testi.YourOrderActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" />
    <activity
        android:name="com.test.LocationActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar" />
    <activity
        android:name="com.test.TestSocialWebActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Dialog" >
    </activity>
    <activity
        android:name="com.test.TestWebActivity"
        android:screenOrientation="portrait" >
    </activity>    
    <activity
        android:name="com.test.WalletWebview"
        android:label="@string/title_activity_wallet_webview"
        android:screenOrientation="portrait" >
    </activity>
</application>
</manifest>

1 个答案:

答案 0 :(得分:1)

public ActionBar getActionBar ()

Added in API level 11
Retrieve a reference to this activity's ActionBar.

Returns

The Activity's ActionBar, or null if it does not have one.

getActionBar()返回null

getActivity()返回与此片段关联的Activity。

所以看起来你应用了一个没有动作栏的主题

编辑:

确认您的问题
android:theme="@android:style/Theme.NoTitleBar"

应用提供操作栏的主题。改为

android:theme="@android:style/Theme.Light"