我的DIALOG中有一个微调器,我想在单击OK按钮时选择该项目。 每次我在对话框中按“确定”。总是出现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)
答案 0 :(得分:1)
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String Text = mySpinner.getSelectedItem().toString();
答案 1 :(得分: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) {
}
}
答案 2 :(得分:0)
你的监听器代码不应该在OnClick of Dialogue中。将作用域分开。
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();
}});