【发布时间】:2015-07-18 20:15:09
【问题描述】:
我有以下两个包含 TextView 和 Button 的 Fragment:
public class MyFragmentText extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final MyFragmentText newInstance(String message)
{
MyFragmentText f = new MyFragmentText();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.layout_text, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);
return v;
}
}
和
public class MyFragmentButton extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final MyFragmentButton newInstance(String message)
{
MyFragmentButton f = new MyFragmentButton();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.layout_button, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);
return v;
}
}
我有三个 XML 文件:
主要布局:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
按钮布局
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button"
android:width="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here"/>
</RelativeLayout>
文本视图布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
现在,当我从主程序调用它们以使用以下代码显示滑动窗口时,它运行良好。
公共类 PageViewActivity 扩展 FragmentActivity {
MyPageAdapter pageAdapter;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
//btn = (Button) findViewById(R.id.button);
//btn.setText("Hello World!!!");
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragmentButton.newInstance("Fragment 1"));
fList.add(MyFragmentText.newInstance("Fragment 2"));
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
但是如果我申请了
btn = (Button) findViewById(R.id.button);
btn.setText("Hello World!!!");
它给了我空指针异常。我想这是因为当我调用 btn 时,thet lauyout 没有膨胀。如果有人能提出任何解决方案,我将不胜感激。
【问题讨论】:
-
你能准确显示activity_page_view xml布局吗?
-
Mightyseal,我刚刚更新了我的帖子以显示三个布局文件
标签: android layout nullpointerexception layout-inflater