以前我有一个活动布局,现在我试图在片段中复制它作为 viewPager 的一部分。我复制了旧的 XML 文件并更改了所有 ID,以防发生冲突。我正在为此片段引用新的 XML 布局并创建新的 ID。但是当我运行它时它会得到这个空指针异常。
这是 logcat 文件 http://i48.tinypic.com/14ekq6s.png
这是发生错误的java文件的一部分。
@Override
public RelativeLayout onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
inflater.inflate(R.layout.one, null);
ViewPager viewFinder = (ViewPager) getView();
//setContentView(R.layout.one);
title = (TextView) viewFinder.findViewById(R.id.frag_AA); //ERROR xml id file probz
这是布局文件。
<?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" >
<TextView
android:id="@+id/frag_AA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="I Am Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>