0

有人将此问题标记为重复(什么是 NullPointerException,我该如何解决?),但这根本没有帮助。我正在重新发布问题。

//

我只是在自学,对 android 编程和 java 还是很陌生。我正在尝试从位于内部存储中的 txt 文件中获取列表。

这就是我所做的。

            try {
                path = getFilesDir().getAbsolutePath() ;
                FileInputStream inFs = new FileInputStream(new File(path.toString() + "/filesitem_" + Type + ".txt"));
                byte[] txt = new byte[inFs.available()];
                while (inFs.read(txt) != -1) {}
                String[] list_item = new String(txt).split(",");

                item_actv = (MultiAutoCompleteTextView) dlgView.findViewById(R.id.Item_AutoTxtView);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, list_item);
                MultiAutoCompleteTextView.CommaTokenizer token = new MultiAutoCompleteTextView.CommaTokenizer();
                item_actv.setTokenizer(token);
                item_actv.setAdapter(adapter);
            } catch (IOException e) {
                Toast.makeText(Activity_list.this, "No item list.", Toast.LENGTH_SHORT).show();
            }

            itemQuant_np = (NumberPicker) dlgView.findViewById(R.id.ItemQuant_Np);
            itemQuant_np.setMinValue(1);
            itemQuant_np.setMaxValue(10);

            SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd", Locale.KOREA);
            Date current = new Date();
            date = formater.format(current);

            AlertDialog.Builder dlg2 = new AlertDialog.Builder(Activity_list.this)
                    .setTitle("New List")
                    .setView(dlgView)
                    .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            try {
                                path = getFilesDir().getAbsolutePath() + "/" + Type + "_list";
                                FileOutputStream outFs = new FileOutputStream(new File(path + "/" + date + " list_" + Type + ".txt"), true);
                                listContent = item_actv.getText().toString() + "\t\t\t" + Integer.toString(itemQuant_np.getValue()) + "\n";
                                outFs.write(listContent.getBytes());
                                outFs.close();
                                Toast.makeText(Activity_list.this, "Saved.", Toast.LENGTH_SHORT).show();
                            } catch (IOException e) {
                                Toast.makeText(Activity_list.this, "The file is already exist.", Toast.LENGTH_SHORT).show();
                            }
                        }
                    })
                    .setNegativeButton("Cancel", null);
            dlg2.show();

当我单击菜单上的项目时,会显示此对话框。

它应该做的是显示 diglog,当我在 MultiAutoCompleteTextView 中输入项目名称时,它会显示可用列表,该列表来自已保存在内部存储中的 txt 文件。在 txt 文件中,有以逗号分隔的项目列表(例如:apple、grape、orange)。如果我在填写 MultiAutoCompleteTextView 并设置项目数量后点击对话框上的确认按钮,它将在指定路径中创建一个新的 txt 文件。

diglog 正确显示,但问题是

  1. 当我点击对话框上的“确认”按钮时应用程序停止(不幸的是,应用程序已停止)

(java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.text.Editable android.widget.MultiAutoCompleteTextView.getText()')

  1. 似乎没有获得 MultiAutoCompleteTextView 的自动完成列表
  2. 它创建新的txt文件,但内容为空

我真的需要你的帮助 :(

+Activity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Manage snacks");
        setContentView(R.layout.subactivity);

        Intent intent = getIntent();
        Type = intent.getStringExtra("Type");

        fileList = (ListView) findViewById(R.id.FileList);

        arFiles = new ArrayList<>();

        File file = new File(getFilesDir().getAbsolutePath() + "/" + Type + "_list");
        arFiles.addAll(Arrays.asList(file.list()));

        flAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arFiles);
        fileList.setAdapter(flAdapter);
        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = arFiles.get(i);
                Intent intent = new Intent(Activity_list.this, Activity_listspec.class);
                intent.putExtra("Type", Type);
                intent.putExtra("fileName", name);
                startActivity(intent);
            }
        });
    }

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, 1, 0, "Add new item");
        menu.add(0, 2, 0, "Add new list");

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                dlgView = View.inflate(Activity_list.this, R.layout.dlg_newitem, null);
                edtTxtNewItem = (EditText) dlgView.findViewById(R.id.EdtTxtNewItem);
                AlertDialog.Builder dlg1 = new AlertDialog.Builder(Activity_list.this)
                    .setTitle("New item")
                    .setView(dlgView)
                    .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            try {
                                path = getFilesDir().getAbsolutePath();
                                FileOutputStream outFs = new FileOutputStream(new File(path + "item_" + Type + ".txt"), true);
                                itemList = edtTxtNewItem.getText().toString() + ",";
                                outFs.write(itemList.getBytes());
                                outFs.close();
                                Toast.makeText(getApplicationContext(), edtTxtNewItem.getText().toString() + " Item added.", 0).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    })
                    .setNegativeButton("Cancel", null);
                dlg1.show();

                return true;

            case 2:
                dlgView = View.inflate(Activity_list.this, R.layout.dlg_newlist, null);

                try {
                    path = getFilesDir().getAbsolutePath() ;
                    FileInputStream inFs = new FileInputStream(new File(path.toString() + "/filesitem_" + Type + ".txt"));
                    byte[] txt = new byte[inFs.available()];
                    while (inFs.read(txt) != -1) {}
                    String[] list_item = new String(txt).split(",");

                    item_actv = (MultiAutoCompleteTextView) dlgView.findViewById(R.id.Item_AutoTxtView);
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, list_item);
                    MultiAutoCompleteTextView.CommaTokenizer token = new MultiAutoCompleteTextView.CommaTokenizer();
                    item_actv.setTokenizer(token);
                    item_actv.setAdapter(adapter);
                } catch (IOException e) {
                    Toast.makeText(Activity_list.this, "No existing list.", Toast.LENGTH_SHORT).show();
                }

                itemQuant_np = (NumberPicker) dlgView.findViewById(R.id.ItemQuant_Np);
                itemQuant_np.setMinValue(1);
                itemQuant_np.setMaxValue(10);

                SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd", Locale.KOREA);
                Date current = new Date();
                date = formater.format(current);

                AlertDialog.Builder dlg2 = new AlertDialog.Builder(Activity_list.this)
                        .setTitle("New List")
                        .setView(dlgView)
                        .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                try {
                                    path = getFilesDir().getAbsolutePath() + "/" + Type + "_list";
                                    FileOutputStream outFs = new FileOutputStream(new File(path + "/" + date + " list_" + Type + ".txt"), true);
                                    listContent = item_actv.getText().toString() + "\t\t\t" + Integer.toString(itemQuant_np.getValue()) + "\n";
                                    outFs.write(listContent.getBytes());
                                    outFs.close();
                                    Toast.makeText(Activity_list.this, "Saved.", Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    Toast.makeText(Activity_list.this, "The file is already exist.", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("Cancel", null);
                dlg2.show();

                return true;

        }

        return false;
    }
}

子活动.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/FileList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

dlg_newitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <EditText
        android:id="@+id/EdtTxtNewItem"
        android:hint="상품명"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"/>

</LinearLayout>

dlg_newlist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

        <MultiAutoCompleteTextView
            android:id="@+id/Item_AutoTxtView"
            android:hint="상품명"
            android:completionThreshold="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"/>

        <NumberPicker
            android:id="@+id/ItemQuant_Np"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">
        </NumberPicker>

</LinearLayout>
4

1 回答 1

0

尝试这个

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dlg_newitem, null);

    dialogBuilder.setView(dialogView);
    dialogBuilder.setTitle("New item");

    EditText edtTxtNewItem= (EditText) dialogView.findViewById(R.id.EdtTxtNewItem);
    editText.setText("");
    dialogBuilder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            try {
                path = getFilesDir().getAbsolutePath();
                FileOutputStream outFs = new FileOutputStream(new File(path + "item_" + Type + ".txt"), true);
                itemList = edtTxtNewItem.getText().toString() + ",";
                outFs.write(itemList.getBytes());
                outFs.close();
                Toast.makeText(getApplicationContext(), edtTxtNewItem.getText().toString() + " Item added.", 0).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    dialogBuilder.setNegativeButton("Cancel", null);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
于 2015-12-13T09:21:47.233 回答