2012-04-24 41 views
0

空我有NewTransferActivity类,它扩展TabActivity。代码:findViewById()返回在TabActivity类

public class NewTransferActivity extends TabActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.account_transfer); 

    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec; 

    spec = tabHost.newTabSpec("receiver").setIndicator("Rec").setContent(getReceiverTab()); 
    tabHost.addTab(spec); 

    spec = tabHost.newTabSpec("transfer").setIndicator("Trans").setContent(getTransferTab()); 
    tabHost.addTab(spec); 

} 

private TabContentFactory getReceiverTab(){ 
    TabContentFactory factory = new TabContentFactory() { 
     @Override 
     public View createTabContent(String tag) { 

      ... 

      return layout; 
     } 
    }; 
    return factory; 
} 

private TabContentFactory getTransferTab(){ 
    TabContentFactory factory = new TabContentFactory() { 

     @Override 
     public View createTabContent(String tag) { 
      LinearLayout layout = (LinearLayout) findViewById(R.id.mytrans); 

      if (layout == null){ 
       //layout = new LinearLayout(NewTransferActivity.this); 
       System.out.println("NULL>>>>>>"); 
       return new LinearLayout(NewTransferActivity.this); 
      } 
      return layout; 
     } 
    }; 
    return factory; 
} ... 

我的XML文件,它包含的布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mytrans" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <requestFocus /> 
</EditText> 


<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


<TextView 
    android:id="@+id/textView3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


<TextView 
    android:id="@+id/textView4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

我想加载布局从XML文件中的单个标签,我definded。不幸的是,我得到NullPointerException,因为方法getTransferTab()中布局为null。 'LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans)'返回null。我做错了什么?提前致谢。

回答

1

你还是尝试使用LayoutInflater从XML获取视图层次,

TabContentFactory factory = new TabContentFactory() { 
     @Override 
     public View createTabContent(String tag) { 

      LayoutInflater inflater = LayoutInflater.from(Activity_Name.this); 
      View linearLayout = inflater.inflate(R.layout.mytrans, null); 

      return linearLayout; 
     } 
    }; 
+0

完美的答案,但insread R.id.mytrans我不得不使用R.layout.layout_name。十分感谢。 – problemgenerator 2012-04-24 12:50:19

1

我的第一个想法是,在这条线:

LinearLayout layout = (LinearLayout) findViewById(R.id.mytrans); 

您所呼叫的你TabContentFactory

findViewById方法你需要调用位于Activity该命令的版本。

+0

嗨。我已将此行更改为LinearLayout layout =(LinearLayout)NewTransferActivity.this.findViewById(R.id.mytrans);但结果仍然为空。我也试图在我的Activity类中写一个方法,它只是返回一个LinearLayout:private LinearLayout idRes(){LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans);返回布局; }并在getReceiverTab()中调用它,但它不正确 – problemgenerator 2012-04-24 12:40:40

+0

在SetContentView之后直接调用findViewById以确保您获得正确的视图。也许你正在加载错误的布局。 – Kuffs 2012-04-24 12:48:31