使用选项菜单时出现异常

时间:2012-10-03 16:53:29

标签: android android-optionsmenu

以下是onOptionsItemSelected回调的代码。我对ID为R.id.up的项目存在问题:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.can:
        can = false;
        sel = false;
        FileName.setEnabled(false);
        FileName.setText("");
        String[] values = new String[] {"Open", "Save (As)", "New"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
        return true;
    case R.id.sel:
        can = false;
        sel = false;
        SelDir = CurDir;
        if (SelDir != null)
        {
        try
        {
            file = new File(SelDir, (FileName.getText().toString() + ".txt"));
            w = new FileWriter(file);
            writer = new BufferedWriter(w);
            writer.write(TextEditor.getText().toString());
            writer.close();
        }
        catch (Exception e)
        {
            Toast.makeText(this, "Error:\n" + e.getMessage().toString(), Toast.LENGTH_LONG).show();
        }
        }
        else
        {
            Toast.makeText(this, "Error:\n" + "Please Select A Directory", Toast.LENGTH_LONG).show();
        }
        values = new String[] {"Open", "Save (As)", "New"};
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
        String temp = FileName.getText().toString() + ".txt";
        FileName.setText(SelDir + "/" + temp);
        FileName.setEnabled(false);
        SelDir = null;
        return true;
    case R.id.up:
        if (CurDir != homeDir)
        {
            CurDir = CurDir.substring(0, CurDir.lastIndexOf("/") - 1);
            dir = new File(CurDir);
            values = dir.list(filter);
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

当我按下R.id.up项时,我的应用程序强制关闭。其他两项工作正常。我的代码找不到问题。

编辑: logcat,例外情况为:

10-03 11:57:45.952: E/AndroidRuntime(18460): FATAL EXCEPTION: main
10-03 11:57:45.952: E/AndroidRuntime(18460): java.lang.NullPointerException
10-03 11:57:45.952: E/AndroidRuntime(18460):    at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at java.util.Arrays.asList(Arrays.java:169)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.IPP.EZPadd.EZPaddActivity.onOptionsItemSelected(EZPaddActivity.java:102)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.app.Activity.onMenuItemSelected(Activity.java:2205)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:823)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.view.View$PerformClick.run(View.java:9081)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.os.Handler.handleCallback(Handler.java:587)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.os.Looper.loop(Looper.java:130)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at android.app.ActivityThread.main(ActivityThread.java:3686)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at java.lang.reflect.Method.invokeNative(Native Method)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at java.lang.reflect.Method.invoke(Method.java:507)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-03 11:57:45.952: E/AndroidRuntime(18460):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我改变了:

CurDir = CurDir.substring(0, CurDir.lastIndexOf("/") - 1);

为:

CurDir = CurDir.substring(0, CurDir.lastIndexOf("/"));