空指针异常 - findViewById()

use*_*828 49 android nullpointerexception oncreate findviewbyid

任何人都可以帮我找出这个程序可能存在的问题.在该onCreate()方法中,findViewById()对所有id返回null,这会在以后导致空指针异常.我无法弄清楚为什么findViewById()找不到这个视图.有什么建议?

这是主要代码:

public class MainActivity extends Activity {

    ViewPager pager;
    MyPagerAdapter adapter;
    LinearLayout layout1, layout2, layout3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout1 = (LinearLayout) findViewById(R.id.first_View);
        layout2 = (LinearLayout) findViewById(R.id.second_View);
        layout3 = (LinearLayout) findViewById(R.id.third_View);

        adapter = new MyPagerAdapter();
        pager = (ViewPager) findViewById(R.id.main_pager);
        pager.setAdapter(adapter);
    }

    private class MyPagerAdapter extends PagerAdapter
    {

        @Override
        public int getCount() { 
            return 3;
        }

        @Override
        public Object instantiateItem(ViewGroup collection, int position) {

            LinearLayout l = null;

            if (position == 0 )
            {
                l = layout1;
            }
            if (position == 1)
            {
                l = layout2;
            }

            if (position == 2)
            {
                l = layout3;
            }
                collection.addView(l, position);
                return l;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view==object);
        }

         @Override
         public void destroyItem(ViewGroup collection, int position, Object view) {
                 collection.removeView((View) view);
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

以及相关的XML文件:

activity_main布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#a4c639">


    <android.support.v4.view.ViewPager
                        android:layout_width="match_parent" 
                        android:layout_height="match_parent" 
                        android:id="@+id/main_pager"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

activity_first布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/first_View">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

activity_second布局:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/second_View">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

而activity_third布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/third_View">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Ahm*_*ck' 80

findViewById()返回一个View,如果它在你提供的布局中存在setContentView(),否则返回null,这就是你发生的事情.

示例如果你setContentView(R.layout.activity_first);然后调用findViewById(R.id.first_View);它将返回一个View,这是你的布局.

但是如果你调用findViewById(R.id.second_View);它会返回,null因为你的activity_first.xml布局中没有一个视图被调用@+id/second_View.

  • 当我在除当前显示视图之外的其他视图上使用它时,出现了该视图.为了克服这种不必要的行为,我在不使用setContentView的情况下引用了视图,如下所示.layout1 =(LinearLayout)View.inflate(this,R.layout.first_View,null); 然后要访问其中的任何内容,我可以使用Button botton =(Button)layout1.findViewById(R.id.button1); 只需添加以便它可能对sombody有帮助. (5认同)