无法将项目添加到微调器

时间:2018-12-01 21:19:49

标签: java android dialog spinner

我无法从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) 

1 个答案:

答案 0 :(得分:0)

view.findViewById(R.id.fromAccountSpinner);更改为dialog.findViewById(R.id.fromAccountSpinner);