Select Spinner item value

2019-04-16 17:01发布

问题:

So I have this spinner where I want to find what the selected item is. I got something in place to remember what the item which is selected. Now there is a button to click within the same layout where the spinner is, this will bring up a dialog box which tells which item you have selected. But I am getting error , on my log cat it says "java.lang.NullPointerException"

This is my XML file for the Dialog Box;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffc0c0c0">

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Saving&apos;s Account"
            android:id="@+id/spinnerSelectedText"
            android:textColor="#ff000000"
            android:drawableLeft="@drawable/transferaccount_icon"
            android:drawablePadding="10dp"
            android:paddingLeft="10dp"
            android:layout_marginTop="10dp"
            android:textSize="16sp" />


</RelativeLayout>

This is my java file which is for the dialog ;

public class examplefile extends DialogFragment {

public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inf = getActivity().getLayoutInflater();
    View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
    builder.setView(theDIalog);

    builder.setCancelable(true);

return dialog;

}

}

Now this is the screen that you get when you start off - well after the main activity class but I don't want to share the code from main activity since i believe it's not necessary;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.makea_transfer, container, false);


    spinner = (Spinner)v.findViewById(R.id.spinnermakeatransfer);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.accounts,R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_items);
    spinner.setAdapter(adapter);
    spinner.setPrompt("Select an item");



Spinner spinner1 = (Spinner)v.findViewById(R.id.spinnermakeatransfer);
    String spinnerSelectedItem = spinner1.getSelectedItem().toString();


    TextView spinnerText = (TextView) v.findViewById(R.id.spinnerSelectedText);
    spinnerText.setText(spinnerSelectedItem);

return v;

}

Now this is the makea_transfer 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">

<Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnermakeatransfer"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:paddingTop="5dp"
            android:spinnerMode="dialog"
            android:layout_marginTop="5dp"
            android:touchscreenBlocksFocus="false" />

</RelativeLayout>

Please can someone help me fix this issue, please comment if you don't understand or need more details from me.

This is the Log Cat Click Here

回答1:

Here :

TextView spinnerText = (TextView) v.findViewById(R.id.spinnerSelectedText);

spinnerSelectedText is inside makea_transfer_confirm layout but trying to get from makea_transfer :

Get TextView in onCreateDialog method of examplefile DialogFragment :

View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
builder.setView(theDIalog);
// get TextView here
 TextView spinnerText = (TextView) theDIalog.findViewById(R.id.spinnerSelectedText);

And for showing spinnerSelectedItem value in spinnerText pass it using examplefile class using setArguments :

v.findViewById(R.id.maketransferReviewButton).setOnClickListener(
                                                new View.OnClickListener() { 
@Override public void onClick(View v) { 
  SampleDialog fragment = new examplefile(); 
  Bundle args = new Bundle();
  args.putString("spinnerText", spinner1.getSelectedItem().toString());
  fragment.setArguments(args);
  examplefile.show(getFragmentManager(), "make a transfer dialog"); 
 } 

});

and inside onCreateDialog get String using getArguments as:

String strSelectedValueFromSpinner = getArguments().getString("spinnerText");
View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
builder.setView(theDIalog);
// get TextView here
TextView spinnerText = (TextView) theDIalog.findViewById(R.id.spinnerSelectedText);
spinnerText.setText(strSelectedValueFromSpinner);


回答2:

You need to do like this

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                String spinnerSelectedItem = spinner.getSelectedItem().toString();
                Log.i("Selected item : ", items);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

and Delete

Spinner spinner1 = (Spinner)v.findViewById(R.id.spinnermakeatransfer);