Reputation: 69
I have NewTransferActivity class, which extends TabActivity. The code:
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;
} ...
my xml file, which contains layout is:
<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>
I want to load layout for a single tab from xml file which I definded. Unfortunatelly I am getting NullPointerException because layout is null in method getTransferTab(). 'LinearLayout layout = (LinearLayout) findViewById(R.id.mytrans)' returns null. What I am doing wrong? Thanks in advance.
Upvotes: 0
Views: 669
Reputation: 67296
You should rather try to use LayoutInflater
to get the view hierarchy from the 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;
}
};
Upvotes: 1
Reputation: 35661
My first thoughts are that, in this line:
LinearLayout layout = (LinearLayout) findViewById(R.id.mytrans);
you are calling the findViewById
method of your TabContentFactory
You need to call the version of that command that is located in the Activity
.
Upvotes: 1