Reputation: 339
Here's the code I have so far.
playername.xml
<?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:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif"
android:paddingTop="@dimen/fab_margin"
android:text="Choose the server: "
android:textAlignment="center"
android:textColor="@color/black"
android:textStyle="bold" />
<Spinner
android:id="@+id/serverspinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/servers_array">
</Spinner>
</LinearLayout>
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@string/insert_name"
android:maxLines="1" />
</LinearLayout>
MainActivity.java
....
public void playerStats(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final LayoutInflater inflater = getLayoutInflater();
final View inflator = inflater.inflate(R.layout.playername, null);
final Spinner spinner = (Spinner) findViewById(R.id.serverspinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(),
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.servers_array));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
builder.setView(inflator)
.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String server = spinner.getSelectedItem().toString();
EditText userNameText = (EditText) inflator.findViewById(R.id.username);
String userName = userNameText.getText().toString();
Intent intent = new Intent(MainActivity.this,PlayerStats.class);
intent.putExtra("nume",userName);
intent.putExtra("sever",server);
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
;
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
In my layout.xml (not shown because it's not important), I have a button that starts "playerStats" function from my activity which brings up an alertdialog window where I give some input. In this window, I have a spinner and an edittext and I can't get the spinner to work properly (to work properly means to be able to get the string selected from the array - servers_array).
From the line marked in MainActivity, I get this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
Upvotes: 2
Views: 970
Reputation: 339
I managed to get it to work.
Replace this line:
final Spinner spinner = (Spinner) findViewById(R.id.serverspinner);
With this one:
final Spinner spinner = (Spinner) inflator.findViewById(R.id.serverspinner);
Upvotes: 2