0

我的 DIALOG 中有一个微调器,我想在单击“确定”按钮时选择该项目。每次我在对话框中按“确定”。NullPointerException 总是出现。

注意:我的微调器位于对话框中。

 builder.setView(inflater.inflate(R.layout.dialog_sendtransfer_layout, null))
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {                         
                       fromSpin.setOnItemSelectedListener(new OnItemSelectedListener() {                          
                        @Override
                        public void onItemSelected(AdapterView<?> arg0,
                                View arg1, int arg2, long arg3) {
                            fromSite = arg0.getItemAtPosition(arg2).toString();

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }  });
                       //fromSite = fromSpin.getItemAtPosition(fromSpin.getSelectedItemPosition()).toString();
                     //toSite = toSpin.getItemAtPosition(toSpin.getSelectedItemPosition()).toString();
                     Toast.makeText(getBaseContext(),"From: " + fromSite, Toast.LENGTH_LONG).show();

                   }

LOGCAT

    02-13 10:57:08.513: E/AndroidRuntime(2430): FATAL EXCEPTION: main
02-13 10:57:08.513: E/AndroidRuntime(2430): java.lang.NullPointerException
02-13 10:57:08.513: E/AndroidRuntime(2430):     at com.aws.ojt.activities.TransferCore$1.onClick(TransferCore.java:132)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at android.os.Looper.loop(Looper.java:137)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at java.lang.reflect.Method.invoke(Method.java:511)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-13 10:57:08.513: E/AndroidRuntime(2430):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String Text = mySpinner.getSelectedItem().toString();
于 2013-02-13T10:31:07.057 回答
0

您的侦听器代码不应位于对话的 OnClick 内。将范围分开。

String str="";
fromSpin.setOnItemSelectedListener(new OnItemSelectedListener() {                          
                    @Override
                    public void onItemSelected(AdapterView<?> arg0,
                            View arg1, int arg2, long arg3) {
                        str= arg0.getItemAtPosition(arg2).toString();

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }  });
                   //fromSite = fromSpin.getItemAtPosition(fromSpin.getSelectedItemPosition()).toString();
                 //toSite = toSpin.getItemAtPosition(toSpin.getSelectedItemPosition()).toString();
                 Toast.makeText(getBaseContext(),"From: " + fromSite, Toast.LENGTH_LONG).show();

               }

// somewhere down below the OnClick goes like..
builder.setView(inflater.inflate(R.layout.dialog_sendtransfer_layout, null))
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {  
//whatever you want to do with "str" here..
Toast.makeText(getBaseContext(),"From: " + str, Toast.LENGTH_LONG).show();

               }});
于 2013-02-13T10:41:29.090 回答
0

尝试这个,

public class SpinnerActivity extends Activity implements OnItemSelectedListener{
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    spinner = (Spinner) findViewById(R.id.spinner);

    //Register a callback to be invoked when an item in this AdapterView has been selected.
    //The argument is the callback that will return
    spinner.setOnItemSelectedListener(SpinnerActivity.this);

    // Adapter for spinner
    ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(getApplicationContext(),  R.array.spinnerItems, android.R.layout.simple_spinner_dropdown_item);
    // Sets the layout resource to create the drop down views
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //The Adapter is used to provide the data which backs this Spinner.
    spinner.setAdapter(adapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    // On selecting a spinner item
    String item = parent.getItemAtPosition(position).toString();

    // showing a toast on selecting an item 
    Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();

}

public void onNothingSelected(AdapterView<?> arg0) {

}

}
于 2013-02-13T10:29:39.380 回答