Android的 - 空指针异常 - findViewById() [英] Android - Null pointer Exception - findViewById()
问题描述
谁能帮我找出可以是问题与此计划。 在onCreate方法的findViewById返回所有IDS空,这将导致一个空指针异常后。我想不通,为什么findViewById找不到视图。任何建议?
这是主要的code:
公共类MainActivity延伸活动{
ViewPager寻呼机;
MyPagerAdapter适配器;
的LinearLayout布局1,布局2,layout3;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
布局1 =(的LinearLayout)findViewById(R.id.first_View);
布局2 =(的LinearLayout)findViewById(R.id.second_View);
layout3 =(的LinearLayout)findViewById(R.id.third_View);
适配器=新MyPagerAdapter();
寻呼机=(ViewPager)findViewById(R.id.main_pager);
pager.setAdapter(适配器);
}
私有类MyPagerAdapter扩展PagerAdapter
{
@覆盖
公众诠释getCount将(){
返回3;
}
@覆盖
公共对象instantiateItem(ViewGroup中收集,INT位置){
的LinearLayout L = NULL;
如果(位置== 0)
{
L =布局1;
}
如果(位置== 1)
{
L =布局2;
}
如果(位置== 2)
{
L = layout3;
}
collection.addView(升,位);
返回L;
}
@覆盖
公共布尔isViewFromObject(查看视图,Object对象){
返程(查看==对象);
}
@覆盖
公共无效destroyItem(ViewGroup中收集,INT位置,对象视图){
collection.removeView((视图));
}
}
}
和相关的XML文件:
activity_main布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:后台=#a4c639>
< android.support.v4.view.ViewPager
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / main_pager/>
< / LinearLayout中>
activity_first布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / first_View>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/参考hello world/>
<按钮
机器人:ID =@ + ID /按钮1
风格=机器人:ATTR / buttonStyleSmall
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=按钮/>
< / LinearLayout中>
activity_second布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / second_View>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/参考hello world/>
< / LinearLayout中>
和activity_third布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / third_View>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/参考hello world/>
< / LinearLayout中>
findViewById()
返回查看,如果它存在于你所提供的布局的setContentView()
,否则返回空值,这就是发生在你身上。
例如,如果您的setContentView(R.layout.activity_first);
,然后调用 findViewById(R.id.first_View);
它会返回一个视图这是你的布局,但如果你调用 findViewById(R.id.second_View);
将返回null,因为没有一个视图你的'activity_first布局称为second_View P>
Can anyone help me to find out what can be the issue with this program. In the onCreate method the findViewById returns null for all ids and this causes a nullpointer exception later. I can not figure out why the findViewById can not find the view . Any suggestion ?
this is the main code :
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);
}
}
}
and the related xml files :
activity_main layout :
<?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>
activity_first layout:
<?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>
activity_second layout :
<?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>
and the activity_third layout :
<?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()
returns a View if it exists in the layout you provided in setContentView()
, otherwise it returns null and that's what happening to you.
Example if you setContentView(R.layout.activity_first);
and then call findViewById(R.id.first_View);
it will return a View which is your layout, But if you call findViewById(R.id.second_View);
it will return null since there is not a view in your 'activity_first' layout called 'second_View'
这篇关于Android的 - 空指针异常 - findViewById()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!