私はアクションバーにスピナーを実装しようとしています。レイアウトでXMLを使用してスピンナーを追加した後、基本アクティビティクラス(activity_main_layout.xml)にアダプタを設定しようとしています。Spinner in Actionバーの結果がNULLポインタの例外です
public class MainLayoutActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String DB_NAME = "yourdbsqlite3.db";
private SQLiteDatabase database;
private String TABLE_NAME="ENGLISH",SongTittle, hymn_number_selected = "702";
private ArrayList HymNumber_Array,HymtitleSearch_Array,songNumber_Array;
private ScrollView song_scrolview ;
private TextView song_textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Controls Defination
song_scrolview = (ScrollView) findViewById(R.id.song_scrollview);
song_textview = (TextView) findViewById(R.id.song_textview);
//DB key helper - initialize SQL lite DB
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.getReadableDatabase();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_layout, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void fillHymNumber(String lang) {
Spinner spinner2 = (Spinner) findViewById(R.id.song_number_spinner);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_english) {
fillHymVerses("english","7");
} else if (id == R.id.nav_tsonga) {
fillHymVerses("tsonga","7");
} else if (id == R.id.nav_sotho) {
fillHymVerses("sotho","7");
} else if (id == R.id.nav_venda) {
fillHymVerses("venda","7");
} else if (id == R.id.nav_zulu) {
fillHymVerses("zulu","7");
} else if (id == R.id.nav_pedi) {
fillHymVerses("pedi","7");
} else if (id == R.id.nav_share) {
fillHymVerses("share","7");
} else if (id == R.id.nav_send) {
fillHymVerses("send","7");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
spinnerlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/song_number_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
activity_main_layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_english"
android:icon="@drawable/ic_menu_camera"
android:title="English" />
<item
android:id="@+id/nav_tsonga"
android:icon="@drawable/ic_menu_gallery"
android:title="Tsonga" />
<item
android:id="@+id/nav_sotho"
android:icon="@drawable/ic_menu_slideshow"
android:title="Sotho" />
<item
android:id="@+id/nav_venda"
android:icon="@drawable/ic_menu_manage"
android:title="Venda" />
<item
android:id="@+id/nav_zulu"
android:icon="@drawable/ic_menu_slideshow"
android:title="Zulu" />
<item
android:id="@+id/nav_pedi"
android:icon="@drawable/ic_menu_manage"
android:title="Pedi" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_spinner"
android:orderInCategory="100"
android:title="728"
app:showAsAction="always"
android:actionLayout="@layout/spinnerlayout"/>
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:title="Search"
app:showAsAction="never" />
<item
android:id="@+id/action_about"
android:orderInCategory="100"
android:title="About"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_close"
android:orderInCategory="100"
android:title="Close"
app:showAsAction="never" />
</menu>
以下
は主な活動であります
エラーログを過ぎてください。 – humazed
致命的な例外:メインプロセス:za.co.theholdings.churchofchristhymns、PID:9361 java.lang.NullPointerException:nullオブジェクトに仮想メソッド 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'を呼び出そうとしていますza.co.thendholdings.churchofchristhymns.MainLayoutActivity.fillHymVersesでza.co.thendholdings.churchofchristhymns.MainLayoutActivity.fillHymNumber(MainLayoutActivity.java:122)(MainLayoutActivity.java:192) – Terrence