2012-07-09 95 views
0

我正在嘗試創建一個記事本應用程序,該應用程序在頂部有三個選項卡,每個都鏈接到不同的視圖。當調用addView()時出現空指針異常:Android

前兩個選項卡將只包含一個表單來填寫,而第三個選項卡將是實際寫入的位置。問題是,每當我試圖儘量擡高我得到一個空指針異常此行此第三種觀點:

((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0); 

這裏是我的代碼:

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, show the tab contents in the container 
    Fragment fragment = new Section3Fragment(); 
    Bundle args = new Bundle(); 
    args.putInt(Section3Fragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); 
    fragment.setArguments(args); 
    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.container3, fragment) 
      .commit(); 
} 


public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 


public class Section3Fragment extends Fragment { 
    public Section3Fragment() { 
    } 

    int section; 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

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

     Bundle args = getArguments(); 
     section = args.getInt(ARG_SECTION_NUMBER); 
     View view; 

     if (section == 1) 
     { 
      view = inflater.inflate(R.layout.section3_page1, container,false); 

      return view; 
     } 
     if (section == 2){ 

     view = inflater.inflate(R.layout.section3_page2, container, false); 
     return view; 
     } 
     else { 


      if(v != null) 
       Log.v("not null", "not null"); 

      view = inflater.inflate(R.layout.section3_page3, container, false); 
      ((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0); //null pointer exception here!! 


      return view; 

     } 
    } 
} 

對象v是上一個實例我用來做實際繪畫的課程。

而且section3_page3佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawRoot" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
</LinearLayout> 

任何深入瞭解這個問題是極大的讚賞。

+0

請問你能在這裏粘貼異常信息嗎? 同樣在你的代碼中,我們看不到** v **被初始化的地方。 – comprex 2012-07-09 15:49:26

回答

2

快速瀏覽,你試圖做的事:

((LinearLayout) view.findViewById(R.id.drawRoot)).addView(v,0); 

你尋找你的片段的視圖。而不是你在if語句中膨脹的觀點。

+0

是的,那是我的問題。謝謝。我從來沒有想過那一個 – sean9898 2012-07-09 15:54:09

+0

我會盡快接受答案 – sean9898 2012-07-09 15:54:36

1

您發佈了section3_page3.xml,但您打開了inflater.inflate(R.layout.section3_page2, container, false)。這是一個錯字還是問題的根源?

打開錯誤的XML文件將導致findViewById()爲null回到這裏:

view = inflater.inflate(R.layout.section3_page2, container, false); 
((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0); 

因此一個NullPointerException ......我猜你的意思是:

view = inflater.inflate(R.layout.section3_page3, container, false); 
((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0); 
+0

這是一個錯字,我很抱歉。它應該是R.layout.section3_page3 – sean9898 2012-07-09 15:51:09

0

既然你使用ActionBar和Fragments,我建議改變這個:

((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0); 

into this:

((LinearLayout) getActivity().findViewById(R.id.drawRoot)).addView(v,0);