NullPointerException:尝试在空对象引用上调用虚拟方法 findViewById(int)'



我根据谷歌官方教程编写了关于碎片的代码
两者几乎相同,但为什么我仍然出现找不到TextView的错误。它由四个java类和三个布局文件组成。我可能想在风景上工作,棘手的部分是在风景上,为肖像作品找。因此,为了便于阅读,我只介绍横向代码。

activity_main.xml(陆地)

正如演示所做的那样,我的布局仍然是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/list_fragment"
        class="com.example.jimjay.bookapp.List_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <fragment
        android:id="@+id/content_fragment"
        class="com.example.jimjay.bookapp.ContenFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

content_view.xml

这是显示文本

<TextView
    android:id="@+id/container_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:textSize="12dp"
    android:textStyle="bold"
    xmlns:android="http://schemas.android.com/apk/res/android" />

MainActivity.java

public class MainActivity extends AppCompatActivity implements List_fragment.OnListSelectedListener{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (findViewById(R.id.container) != null) {
                if (savedInstanceState != null) {
                    return;
                }
                List_fragment firstFragment = new List_fragment();
                firstFragment.setArguments(getIntent().getExtras());
                getSupportFragmentManager().beginTransaction().add(R.id.container, firstFragment).commit();
            }
        }
        @Override
        public void onChapterSelected(int position) {
            ContenFragment contenFragment=(ContenFragment)getSupportFragmentManager().findFragmentById(R.id.content_fragment);
            if (contenFragment != null){
                contenFragment.updateContent(position);
            }else {
                ContenFragment newFragment = new ContenFragment();
                Bundle args = new Bundle();
                args.putInt(ContenFragment.ARG_POSITION, position);
                newFragment.setArguments(args);
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.container, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        }
    }

List_fragment.java

public class List_fragment extends ListFragment {
        OnListSelectedListener mCallBack;
        interface OnListSelectedListener{
            void onChapterSelected(int position);
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            int layout=android.R.layout.simple_list_item_activated_1;
            setListAdapter(new ArrayAdapter<>(getActivity(),layout,new Book_content().chapters));
        }
        @Override
        public void onStart() {
            super.onStart();
            if (getFragmentManager().findFragmentById(R.id.list_fragment) != null){
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            }
        }
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            try {
                mCallBack=(OnListSelectedListener) context;
            }catch (ClassCastException e){
                throw new ClassCastException(context.toString()+" must implement OnListSelectedListener");
            }
        }
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            mCallBack.onChapterSelected(position);
            getListView().setItemChecked(position,true);
        }
    }

ContenFragment.java

public class ContenFragment extends Fragment {
final static String ARG_POSITION="position";
int mCurrentPosition=-1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (savedInstanceState != null){
        mCurrentPosition=savedInstanceState.getInt(ARG_POSITION);
    }
    return inflater.inflate(R.layout.content_view,container,false);
}
@Override
public void onStart() {
    super.onStart();
    Bundle args=getArguments();
    if (args!= null){
        updateContent(args.getInt(ARG_POSITION));
    }else if (mCurrentPosition != -1){
        updateContent(mCurrentPosition);
    }
}
public void updateContent(int position) {
    TextView textView = (TextView) getActivity().findViewById(R.id.container_content);
    if (textView != null) {
        textView.setText((new Book_content().contents_string[position]));
    }else {
        Log.e("LOG", "The textView is null");
    }
    mCurrentPosition = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(ARG_POSITION,mCurrentPosition);
}

}

最后一个Java文件是Book_content.class,它只包含字符串。所以它在肖像中起作用,但在风景中不起作用,我搞不清楚,我发现的问题是ContenFragment.class中的TextView引发了异常,它是空的。但我确实在content_view中有一个TextView。为什么?帮助

我自己想弄清楚,现在我明白了。奇怪的是,它仍然遇到异常,即使我只是直接在类中粘贴了官方示例代码。所以我不得不转向其他实例化。将右侧的片段更改为fragmentLayout,使其与纵向布局相同。

activity_main.xml(陆地)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/list_fragment"
        class="com.example.jimjay.bookapp.List_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <FrameLayout
        android:id="@+id/content_fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"></FrameLayout>
</LinearLayout>

因此,只需使用FragmentTransaction来添加contentFragment即可。

相关内容

最新更新