2012-07-09 94 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);