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

时间:2016-04-14 10:03:26

标签: android xml android-layout android-fragments

我根据Google official tutorial发了关于片段的代码     并且它们两者几乎相同,但为什么我仍然遇到无法找到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,它只包含字符串。   所以它以纵向工作,但不是在横向上工作,我无法弄明白,而我发现的问题是TextView中的ContenFragment.class引发了异常,它是空的。但我在TextView中确实有content_view。这是为什么?帮助

1 个答案:

答案 0 :(得分:0)

我自己想弄清楚,现在我做了。 奇怪的是它仍然遇到了EXCEPTION,即使我只是直接在我的类中粘贴官方示例代码。我必须转向其他实例化。将右侧的片段更改为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即可。