0

我在 Google Play 开发者控制台中收到了这个崩溃报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ZhuangDictActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:138)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ZhuangDictActivity.showChooseDictDialog(ZhuangDictActivity.java:560)
at ZhuangDictActivity.onCreate(ZhuangDictActivity.java:339)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
... 11 more

这是在 onCreate 下:

sharedPreferences = getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
            dictName = sharedPreferences.getString(PREFERENCES_DICT_NAME, null);
if (dictName == null) {
                showChooseDictDialog(false);
            } else {
                modifyDictVar(dictName);
                setTitle(loadDictInfo());

                initDbFile();
                databaseHelper.initDb();
            }

showChooseDictDialog:

private void showChooseDictDialog(boolean cancelable) {
        AlertDialog.Builder dictChooseBuilder = new AlertDialog.Builder(this);
        FilenameFilter filter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                return filename.contains(".ifo");
            }
        };
        dictFilenames = dictPathFile.list(filter);
        if (dictFilenames.length > 0) {
            dictChooseBuilder.setItems(dictFilenames, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dictName = dictFilenames[which].substring(0, dictFilenames[which].indexOf(".ifo"));

                    modifyDictVar(dictName);
                    initDbFile();
                    databaseHelper.initDb();

                    if (new File(ifoFileName).isFile()) {
                        setTitle(loadDictInfo());
                    }

                    if (new File(idxFileName).isFile() && dictFile.isFile()) {
                        sharedPreferences.edit().putString(ZhuangDictActivity.PREFERENCES_DICT_NAME, dictName).commit();

                        // dbHelper.dropTable(tableName);
                        if (!databaseHelper.isTableExist(tableName)) {
                            databaseHelper.beginTransaction();
                            databaseHelper.createTable(tableName);
                            for (char c = 'a'; c <= 'z'; c++) {
                                String temp = DatabaseHelper.transTableName(c);
                                databaseHelper.createTable(temp);
                            }
                            databaseHelper.setTransactionSuccessful();
                            databaseHelper.endTransaction();
                            new LoadDictIndexAsyncTask().execute(dictName);
                        }
                    } else {
                        showDialog(getResources().getString(R.string.choose_dict_content));
                    }
                }   
            });
            dictChooseBuilder.setCancelable(cancelable).setTitle(getResources().getString(R.string.choose_dict_title))
                    .create().show();
        } else {
            dictChooseBuilder.setCancelable(cancelable).setMessage(
                    getResources().getString(R.string.choose_dict_content)).create().show();
        }
    }

ZhuangDictAcitivity.java 的完整源代码在这里:https ://code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java

有谁知道这个的解决方案?

4

1 回答 1

0

根据评论,问题在这里:

dictFilenames = dictPathFile.list(filter);
if (dictFilenames.length > 0) {

File.list()null例如,如果File不是目录,则可能返回。访问数组的length字段null会导致NullPointerException. 以下更改可能会有所帮助:

dictFilenames = dictPathFile.list(filter);
if (dictFileNames != null && dictFilenames.length > 0) {
于 2013-10-14T13:03:09.260 回答