Thursday, 24 November 2016

android - Null pointer Exception - findViewById()



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 null pointer exception later. I can not figure out why the findViewById() can not find the view. Any suggestions?



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:





android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#a4c639">


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




activity_first layout:




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


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

android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Button" />




activity_second layout:



 


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

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





And the activity_third layout:




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


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



Answer



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.xml layout called @+id/second_View.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...