2014-02-28 140 views
-2

所以我只是學習編寫android應用程序,沒有具體的,但主要圍繞着看他們是如何工作的。我嘗試使用操作欄中的選項卡執行操作,基於我找到的教程,但在應用程序啓動之前,我得到一個空指針異常。Android空指針異常onCreate

下面是代碼:

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

    viewPager = (ViewPager) findViewById(R.id.pager); 
    actionBar = getActionBar(); 
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

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

    for(String tab_names : tabs) 
    { 
     actionBar.addTab(actionBar.newTab().setText(tab_names).setTabListener(this)); 
    } 

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() 
    { 
     @Override 
     public void onPageSelected(int position) 
     { 
      actionBar.setSelectedNavigationItem(position); 
     } 

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

     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) 
     { 

     } 
    }); 
} 

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

    View rootView = inflater.inflate(R.layout.web_fragment, container, false); 

    return rootView; 
} 

有2個不同的佈局的其他類的onCreateView的但是完全一樣的說明。下面是XML,第一是主要的和未來的片段:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/pager" 
android:layout_width="match_parent" 
android:layout_height="match_parent"/> 



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="#ff8400"> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/hello_world" 
    android:textSize="20sp" 
    android:layout_centerInParent="true"/> 

我似乎無法找出是什麼原因造成拋出的異常。我也似乎無法得到logcat導出到日誌文件,所以我會嘗試在這裏得到它。

logcat的

02-28 01:36:42.431: I/Process(1629): Sending signal. PID: 1629 SIG: 9 

    02-28 01:37:27.401: D/AndroidRuntime(1668): Shutting down VM 

    02-28 01:37:27.401: W/dalvikvm(1668): threadid=1: thread exiting with uncaught exception (group=0xb4a1fb90) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): FATAL EXCEPTION: main 

    02-28 01:37:27.471: E/AndroidRuntime(1668): Process: com.example.tolerablenausea, PID: 1668 

    02-28 01:37:27.471: E/AndroidRuntime(1668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tolerablenausea/com.example.tolerablenausea.MainActivity}: java.lang.NullPointerException 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.ActivityThread.access$700(ActivityThread.java:135) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.os.Handler.dispatchMessage(Handler.java:102) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.os.Looper.loop(Looper.java:137) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.ActivityThread.main(ActivityThread.java:4998) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at java.lang.reflect.Method.invokeNative(Native Method) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at java.lang.reflect.Method.invoke(Method.java:515) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
    02-28 01:37:27.471: E/AndroidRuntime(1668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at dalvik.system.NativeStart.main(Native Method) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): Caused by: java.lang.NullPointerException 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.content.ContextWrapper.getResources(ContextWrapper.java:89) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.content.Context.getString(Context.java:343) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at com.example.tolerablenausea.MainActivity.<init>(MainActivity.java:29) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at java.lang.Class.newInstanceImpl(Native Method) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at java.lang.Class.newInstance(Class.java:1208) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093) 

    02-28 01:37:27.471: E/AndroidRuntime(1668): ... 11 more 
+0

把你的logcat .. – rajshree

+0

顯示logcat的請 – Sanjeev

+3

'爲(字符串tab_names:標籤)' 哪裏'tabs'來從? – indivisible

回答

1

其中每個循環已被使用,無法看到正在初始化的「選項卡」。如果它爲空並且其中沒有「字符串」。

檢查:
如果你的android:minSdkVersion設置爲8(API-8),它不提供getActionBar()(僅在API-11以後)。

如果lint警告被抑制,您將不會得到編譯時錯誤。

嘗試http://actionbarsherlock.com/ - 向後兼容性有幫助。

如果你想檢查一下,看 ,如果這個工程:

if (android.os.Build.VERSION.SDK_INT >= 11) 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
+0

我的minSdkVersion目前是14,目標SDK爲19 – user3363625

+0

@ user3363625請發佈logcat以精確地跟蹤錯誤 – user2450263