所以我有这个微调器,我想找到所选项目的内容。我有一些东西可以记住所选项目。现在有一个按钮可以在微调器所在的同一布局中单击,这将弹出一个对话框,告诉您选择了哪个项目。但我收到错误,在我的日志猫上说它" java.lang.NullPointerException"
这是对话框的XML文件;
<?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'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>
这是我的java文件,用于对话框;
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;
}
}
现在这是你开始时获得的屏幕 - 在主要活动课程之后,但我不想分享主要活动的代码,因为我认为没有必要;
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;
}
现在这是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>
请有人帮我解决此问题,如果您不了解或需要我提供更多详细信息,请发表评论。
这是Log Cat Click Here
答案 0 :(得分:2)
这里:
TextView spinnerText = (TextView) v.findViewById(R.id.spinnerSelectedText);
spinnerSelectedText
位于makea_transfer_confirm
布局内,但试图从makea_transfer
获取:
使用onCreateDialog
DialogFragment的examplefile
方法获取TextView:
View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
builder.setView(theDIalog);
// get TextView here
TextView spinnerText = (TextView) theDIalog.findViewById(R.id.spinnerSelectedText);
要在spinnerSelectedItem
中显示spinnerText
值,请使用setArguments使用examplefile
类传递它:
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");
}
});
并在onCreateDialog中使用getArguments获取String:
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);
答案 1 :(得分:1)
你需要这样做
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) {
}
});
和删除
Spinner spinner1 = (Spinner)v.findViewById(R.id.spinnermakeatransfer);