我正在嘗試創建一個記事本應用程序,該應用程序在頂部有三個選項卡,每個都鏈接到不同的視圖。當調用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>
任何深入瞭解這個問題是極大的讚賞。
請問你能在這裏粘貼異常信息嗎? 同樣在你的代碼中,我們看不到** v **被初始化的地方。 – comprex 2012-07-09 15:49:26