android - Getting java.lang.NullPointerException On simply populating spinner from string array or list or xml file(strings.xml) -


i want populate spinner through string array or through xml file(strings.xml). getting java.lang.nullpointerexception: error. have searched lot on google worthless. have tried below.

secondactivity.java

package com.example.android02.personalityinsight;  import android.os.bundle; import android.support.design.widget.tablayout; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; import android.support.v4.view.viewpager; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.spinner;  public class secondactivity extends appcompatactivity implements view.onclicklistener {      tablayout tabs;     viewpager viewpager;     mypageradapter pageradapter;      spinner sp1, sp2;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_second);  //        string colors[] = {"red", "blue", "white", "yellow", "black", "green", "purple", "orange", "grey"}; //        sp1 = (spinner) findviewbyid(r.id.sp1); // //        arrayadapter<string> spinnerarrayadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, colors); //        spinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); //        sp1.setadapter(spinnerarrayadapter);          sp2 = (spinner) findviewbyid(r.id.sp2);         arrayadapter<charsequence> adapter = arrayadapter.createfromresource(                 this, r.array.ans, android.r.layout.simple_spinner_item);         adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         sp2.setadapter(adapter);          tabs = (tablayout) findviewbyid(r.id.tabs);         viewpager = (viewpager) findviewbyid(r.id.viewpager);         submit = (button) findviewbyid(r.id.btn_submit);          tabs.addtab(tabs.newtab().settext("personality insights"));         tabs.addtab(tabs.newtab().settext("quiz"));         tabs.addtab(tabs.newtab().settext("pi v/s quiz"));          pageradapter = new mypageradapter(getsupportfragmentmanager(), tabs.gettabcount());         viewpager.setadapter(pageradapter);         tabs.settabsfrompageradapter(pageradapter);         tabs.setupwithviewpager(viewpager);     }      @override     public void onclick(view v) {         switch (v.getid()) {             case r.id.btn_submit:                 break;             default:         }     }      private class mypageradapter extends fragmentstatepageradapter {          int mnumoftabs;          public mypageradapter(fragmentmanager fm, int mnumoftabs) {             super(fm);             this.mnumoftabs = mnumoftabs;         }          public mypageradapter(fragmentmanager fm) {             super(fm);         }          @override         public fragment getitem(int position) {             switch (position) {                 case 0:                     return tabone_fragment.newinstance();                 case 1:                     return tantwo_fragment.newinstance();                 case 2:                     return tabthree_fragment.newinstance();                 default:                     return tabone_fragment.newinstance();             }         }          @override         public int getcount() {             return mnumoftabs;         }     } } 

tabtwo_fragment.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="10dp">      <tablelayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:gravity="center_horizontal">          <tablerow             android:id="@+id/row1"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:gravity="center_vertical">              <textview                 android:id="@+id/q1"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:gravity="center"                 android:padding="6dp"                 android:text="sitting , reading: "                 android:textappearance="?android:attr/textappearance" />              <spinner                 android:id="@+id/sp1"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"></spinner>         </tablerow>          <tablerow             android:id="@+id/row2"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:gravity="center_vertical">              <textview                 android:id="@+id/q2"                 android:layout_width="250dp"                 android:layout_height="wrap_content"                 android:gravity="center"                 android:padding="6dp"                 android:text="sitting, inactive in public place (e.g. theatre or meeting): "                 android:textappearance="?android:attr/textappearance" />              <spinner                 android:id="@+id/sp2"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"></spinner>         </tablerow>     </tablelayout> </relativelayout> 

strings.xml

<resources>     <string name="app_name">personalityinsight</string>     <!-- custom -->     <string name="facebook_app_id">xxxxxxxxxxxxxxxx</string>      <string-array name="ans">         <item>zero</item>         <item>one</item>         <item>two</item>         <item>three</item>     </string-array> </resources> 

error image

however getting error @ line sp2.setadapter(adapter); no matter filling spinner array or xml file. have tried filling spinner list same error occurred.

thanks in advance.

the error says adapter null.

try change adapter initialise adapter this:

arrayadapter<charsequence> adapter =      new arrayadapter<charsequence>(this, android.r.layout.simple_spinner_item, r.array.ans); 

edit

actually know what, why not in xml file instead, this:

<spinner        android:id="@+id/sp1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:spinnermode="dropdown"        android:entries="@array/ans"        android:prompt="@string/select_one"/> 

edit2

since prefer programmatically, try:

arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, getresources().getstringarray(r.array.ans));  adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);  sp2.setadapter(adapter); 
  • r.array.ans changed getresources().getstringarray(r.array.ans)

hope helps.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -