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:
<?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<T>(Int32)Finds a view that was identified by the id attribute from the XML layout resource.
Handling the NullPointerException in Android StudioTry: The Try block executes a piece of code that is likely to crash or a place where the exception occurs. Catch: The Catch block will handle the exception that occurred in the Try block smoothly(showing a toast msg on screen) without letting the app crash abruptly.
java.lang.NullPointerException. Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.
findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .
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. Note that if you don't setContentView()
, and don't have a valid view to findViewById()
on, findViewById()
will always return null until you call setContentView()
.
This also means variables in the top-level trigger an NPE, because they're called before onCreate()
, and by extension, before setContentView()
. See also the activity lifecycle
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);
before setContentView()
, it will return null
since there is not a view in your activity_first.xml
layout called @+id/second_View
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With