Android 空指针异常-findViewById()

Android 空指针异常-findViewById(),android,nullpointerexception,oncreate,findviewbyid,Android,Nullpointerexception,Oncreate,Findviewbyid,有人能帮我找出这个计划的问题吗。 在onCreate()方法中,findViewById()为所有ID返回null,这将导致以后出现null指针异常。我不明白为什么findViewById()找不到视图。有什么建议吗 这是主要代码: public class MainActivity extends Activity { ViewPager pager; MyPagerAdapter adapter; LinearLayout layout1, layout2, lay

有人能帮我找出这个计划的问题吗。 在
onCreate()
方法中,
findViewById()
为所有ID返回null,这将导致以后出现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);
         }
    }
}
以及相关的XML文件:

活动单元主布局:

<?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>

活动单元第一布局:

<?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>

活动单元第二布局:

 <?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>

活动的第三个布局:

<?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>

findViewById()
如果视图存在于您在
setContentView()
中提供的布局中,则返回该视图,否则返回null,这就是发生在您身上的情况。请注意,如果您没有
setContentView()
,并且没有打开
findViewById()
的有效视图,
findViewById()
将始终返回null,直到调用
setContentView()

这也意味着顶级中的变量会触发NPE,因为它们是在
onCreate()
之前调用的,扩展来说,是在
setContentView()之前调用的。另见

如果
setContentView(首先是R.layout.activity_)的示例然后调用
findViewById(R.id.first\u视图)它将返回一个视图,该视图是您的布局


但是如果你调用
findviewbyd(R.id.second\u视图)
setContentView()
之前,它将返回
null
,因为在
活动\u first.xml
布局中没有名为
@+id/second\u view的视图

您尝试获取的视图未在
活动\u main
布局中定义。您需要以编程方式扩大您试图添加到寻呼机的视图-

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    LinearLayout l = null;

    if (position == 0) {
        l = (LinearLayout) View.inflate(this, R.layout.activity_first, null);
    }
    if (position == 1) {
        l = (LinearLayout) View.inflate(this, R.layout.activity_second, null);
    }
    if (position == 2) {
        l = (LinearLayout) View.inflate(this, R.layout.activity_third, null);
    }

    collection.addView(l, position);
    return l;
}

在访问这些视图之前,请将它们添加到寻呼机适配器

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

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

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

}
在寻呼机适配器中:

public Object instantiateItem(View collection, int position) {
    if(position == 0){
        View layout = inflater.inflate(R.layout.activity_first, null);

        ((ViewPager) collection).addView(layout);

        return layout;
    } 
    ... and so forth.

}

从这里,您可以通过findViewById访问它们

上面@Warlock所说的是对的,你应该以正确的方式开始线性布局布局1、布局2、布局3:

LinearLayout layout1 = (LinearLayout) View.inflate(this, R.layout.first_View, null);
LinearLayout layout2 = (LinearLayout) View.inflate(this, R.layout.second_View, null);
LinearLayout layout3 = (LinearLayout) View.inflate(this, R.layout.third, null);

希望我的建议能帮助您

在Android中,当您在布局集中查找视图时,
findViewById(R.id.some_id)
会起作用

也就是说,如果设置了布局,请说:

setContentView(R.layout.my_layout);
只能在此布局(my_布局)中找到视图。


在您的代码
layout1
layout2
layout3
中都有三种不同的布局,它们没有设置为活动。

findViewById
方法必须找到布局中的内容(您在
setContentView
中调用了它)

有时您需要在Eclipse中清理项目(Project-Clean.。

我今天遇到了这个错误,清除它是如此简单,以至于我“脸红了”

只需尝试将UI元素添加到res/layout-port目录中的布局xml文件中!!!

已添加

对于活动类中的这些情况

Activity.findViewById(int-id)

onCreate(Bundle)
中处理的XML中查找由
id
属性标识的视图


否则,例如片段、适配器、来自
LayoutInflater
视图等

View.findViewById(int-id)

查找具有给定id的子视图。如果此视图具有给定id,则返回此视图


无论哪种情况

返回
如果找到
null
则显示视图,否则显示


现在,重新检查XML文件。确保在
setContentView
inflater.inflate
中输入了正确的值

对于活动,在
setContentView
之后调用
findViewById


然后,确保使用
android:id=“@+id/…”查找一个视图
在该布局中。确保
+
位于
@+id
,这会将资源添加到
R.id
值中,以确保您可以从Java中找到它。

在我的情况下,这是一个愚蠢的错误。我用OnCreate方法编写代码,但它在
setContentView
代码行上方.一旦我将代码移到这一行以下,应用程序就开始正常工作

setContentView(R.layout.activity_main);

当XML文件中有许多组件的名称相同时,也会产生此问题。即使它们是不同的布局文件,也应该为每个元素指定一个唯一的名称。
出现此错误是因为您在另一个布局文件中有一个同名的XML元素,而android studio正在尝试访问该元素并显示此错误

我已将所有布局包含到主布局中,并且不再存在空指针异常。感谢提示。当我在当前displayin以外的其他视图中使用此选项时为了克服这种不必要的行为,我在不使用setContentView的情况下引用了该视图,如下所示。布局1=(LinearLayout)视图。充气(this,R.layout.first_view,null);然后要访问其中的任何内容,我可以使用Button botton=(Button)layout1.findViewById(R.id.button1);只是添加,以便对sombody有所帮助。为什么?强制我们有什么好处?那么,包含
setContentView()
中未提供的资源的解决方案是什么?您如何访问mainactivity中的视图?