我在从Arraylist向微调器添加项目时遇到问题。我让它在代码的其他部分工作,但当我把它放在对话框中时,它就不起作用了。确切的问题是它会导致应用程序崩溃。当我按下按钮打开对话框时,就会发生崩溃。
以下是有问题的代码:
public void transferButton(View view) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_transfer);
ArrayList<String> file = new ArrayList();
List<String> accounts = new ArrayList();
File path = Environment.getExternalStorageDirectory();
File myExternalFile = new File(path.getAbsolutePath() + folderLoc, "accounts.txt");
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
file.add(strLine);
}
br.close();
in.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
ArrayAdapter<String> adapter2 = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner fromSpinner = (Spinner) view.findViewById(R.id.fromAccountSpinner);
Spinner toSpinner = (Spinner) view.findViewById(R.id.toAccountSpinner);
for (int i = 0; i < file.size(); i++) {
String[] splitArray = file.get(i).split("//");
adapter1.add(splitArray[0]);
adapter2.add(splitArray[0]);
}
adapter1.insert("From...", 0);
fromSpinner.setAdapter(adapter1);
adapter2.insert("To...", 0);
toSpinner.setAdapter(adapter2);
Button dialogButton = (Button) dialog.findViewById(R.id.closeButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
以下是错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gstraatsma.gsfinancetracker, PID: 14493
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.gstraatsma.gsfinancetracker.MainActivity.transferButton(MainActivity.java:1258)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
将view.findViewById(R.id.fromAccountSpinner);
更改为dialog.findViewById(R.id.fromAccountSpinner);