2016-12-26 113 views
-2

我是新手到android和工作在一個演示刷卡選項卡演示和引用鏈接,一步一步走,但我堅持一個點,當我運行該應用程序,它會引發nullpointerexception,請看下面的代碼,並幫助我弄清楚請NullpointerException在Android的操作欄

main_listing.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

的Java

public class MainListingActivity extends FragmentActivity { 
    ViewPager Tab; 
    TabPagerAdapter TabAdapter; 
    ActionBar actionBar; 

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

     TabAdapter = new TabPagerAdapter(getSupportFragmentManager()); 

     Tab = (ViewPager)findViewById(R.id.pager); 
     Tab.setOnPageChangeListener(
       new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 

         actionBar = getActionBar(); 
         actionBar.setSelectedNavigationItem(position);     } 
       }); 
     Tab.setAdapter(TabAdapter); 

     actionBar = getActionBar(); 
     //Enable Tabs on Action Bar 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     ActionBar.TabListener tabListener = new ActionBar.TabListener(){ 

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

      } 

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

       Tab.setCurrentItem(tab.getPosition()); 
      } 

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

      }}; 
     //Add New Tab 
     actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener)); 
     actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener)); 
     actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener)); 

    } 

logcat的

FATAL EXCEPTION: main 
Process: abc.kayraas.com.allaboutcity, PID: 15728 
java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.kayraas.com.allaboutcity/abc.kayraas.com.allaboutcity.MainListingActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5653) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
    at abc.kayraas.com.allaboutcity.MainListingActivity.onCreate(MainListingActivity.java:37) 
    at android.app.Activity.performCreate(Activity.java:5541) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)  
    at android.app.ActivityThread.access$900(ActivityThread.java:172)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:146)  
    at android.app.ActivityThread.main(ActivityThread.java:5653)  
    at java.lang.reflect.Method.invokeNative(Native Method)  

style.xml

<resources> 


    <style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton"> 
     <item name="android:button">@drawable/radio_selected</item> 
    </style> 

    <style name="AppBaseTheme" parent="android:Theme.Light"> 

    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 

     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 
</resources> 

清單

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".SlashActivity" 
      android:theme="@android:style/Theme.Holo.Light.DarkActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
+0

您使用清單中是什麼樣的主題。操作欄已棄用。如果您還想使用支持操作欄的主題,則使用getSupportActionBar()方法。 – Abhishek

+0

看看這個問題https://stackoverflow.com/questions/15206447/android-actionbar-nullpointerexception?rq=1 –

+0

@Abhishek - 我發佈了我的清單和style.can你幫我? –

回答

0

試試這個:

動作條= getSupportActionBar

+0

當我使用getSupportActionBar,它給錯誤。(使getter方法)。 –

+0

請查看此鏈接:http://www.androidhive。信息/ 2015/09/Android的材料 - 設計 - 工作與 - 標籤/ –

+0

uska喜refrence麗雅他BHAI .. !!! –

0
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle("Home"); 

//在styles.xml在AppTheme,更新爲

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 

//在XML頂部寫:

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:visibility="gone" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:visibility="visible" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

這是您當前的代碼:

<style name="AppTheme" parent="AppBaseTheme"> 
    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 

    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

改爲寫下:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 
+0

可能並非基金「工具欄」 –

+0

你有你的XML創建工具欄。 –

+0

如是-u能幫助我嗎?怎麼辦呢 –

0

試試這段代碼。 ActionBar actionBar = getSupportActionBar();

0

在你style.xml改變這一行:

<style name="AppBaseTheme" parent="android:Theme.Light"> 

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">