程序不工作(没有任何错误/警告)

时间:2015-08-11 16:58:29

标签: java android

我很困惑,为什么程序没有响应。我在系统等了2-3分钟说:

  

程序没有响应。你想关闭吗?

所以,我的这是我的代码。

主要活动

private ArrayAdapter<String> client_name_arrayAdapter;
private ArrayList<String> client_name_arrayList = new ArrayList<>();
private LinkedHashMap<String, Integer> client_name_map = new LinkedHashMap<>();


// onCreate() method
client_name_arrayAdapter = new ArrayAdapter<>(Satish.this, android.R.layout.select_dialog_singlechoice);
client_name = (EditText) findViewById(R.id.client_name);

// set fields only readable
client_name.setFocusableInTouchMode(false);
client_name.setKeyListener(null);
View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.client_name:
                        try {
                            /** clear <code>client_name_map</code> to prevent reassigning the elements  */
                            client_name_map.clear();
                            client_name_map = kliyentDAO.getClients();

                            /** clear <code>client_name_arrayList</code> to prevent reassigning the elements  */
                            client_name_arrayList.clear();
                            client_name_arrayList.addAll(client_name_map.keySet());

                            /** clear <code>client_name_arrayAdapter</code> to prevent reassigning the elements  */
                            client_name_arrayAdapter.clear();
                            client_name_arrayAdapter.addAll(client_name_arrayList);

                            AlertDialog.Builder builder_client_name = new AlertDialog.Builder(Satish.this);
                            builder_client_name.setTitle("Malı seçin");
                            if (client_name_arrayAdapter.isEmpty()) {
                                builder_client_name.setMessage("Heç bir müştəri tapılmadı.");
                                builder_client_name.setNegativeButton("İmtina", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                            } else {
                                builder_client_name.setSingleChoiceItems(client_name_arrayAdapter, -1, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        _which[0] = which;
                                        id = client_name_map.get(client_name_arrayList.get(which));
                                    }
                                }).setNegativeButton("İmtina", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setPositiveButton("Seç", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        client_name.setText(client_name_arrayList.get(_which[0]));
                                        dialog.dismiss();
                                    }
                                });
                            }
                            builder_client_name.create().show();
                        } catch (Exception e) {
//                            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                            Log.wtf("Unknown bug: ", e.toString());
                        }
                        break;
                }
            }
}

DAO课程

public LinkedHashMap<String, Integer> getClients() {
    LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();

    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT idkliy, kliyentad FROM " + TABLE_KLIYENT, null);
    cursor.moveToPosition(0);
    while (!cursor.isAfterLast()) {
        linkedHashMap.put(cursor.getString(cursor.getColumnIndex(COLUMN_KLIYENTAD)), cursor.getInt(cursor.getColumnIndex(COLUMN_IDKLIY)));
    }
    cursor.close();

    return linkedHashMap;
}

当用户点击client_name字段时,我想显示一个提示框 警告框应填充 client_name_arrayAdapter。不久,我可以描述上面代码的结构:

  

DAO类的功能

     

程序从本地数据库中获取一些字段值。 linkedHashMap填充了此数据。并且函数返回已填充的(如果数据库中没有数据为空)   LinkedHashMap的。

     

MainActivity的功能

     

本地 linkedHashMap(在上面的代码中命名为map)等于 DAO类函数返回的内容。 client_name_arrayList填充了mapclient_name_arrayAdapter填充了 client_name_arrayList 。最后,数组适配器将adapter设置为alertBuilder

我认为这不是更好或更坏的方式,但应用程序的其他部分正常工作(没有任何问题)。

修改

logcat中没有任何有问题的日志(级别 VERBOSE )。当我单击该字段时,日志:

08-11 22:01:20.625  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1940K, 32% free 6516K/9572K, paused 22ms, total 23ms
08-11 22:01:21.075  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2060K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:21.475  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:21.876  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:22.326  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 24ms, total 24ms
08-11 22:01:22.787  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:23.217  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:23.607  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:24.028  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 33ms, total 33ms
08-11 22:01:24.438  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 34ms, total 34ms
08-11 22:01:24.859  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 33ms, total 33ms
08-11 22:01:25.329  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:25.790  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:26.210  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:26.610  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 34ms, total 34ms
08-11 22:01:27.041  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 36ms, total 36ms
08-11 22:01:27.441  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:27.852  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 26ms, total 26ms
08-11 22:01:28.262  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:28.662  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:29.073  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:29.553  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:30.034  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:30.464  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 42ms, total 42ms
08-11 22:01:30.875  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:31.335  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:31.795  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:32.276  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 35ms, total 35ms
08-11 22:01:32.746  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 26ms, total 26ms
08-11 22:01:33.207  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 26ms, total 26ms
08-11 22:01:33.677  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:34.138  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:34.538  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:34.938  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:35.339  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:35.729  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 33ms, total 33ms
08-11 22:01:36.140  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:36.540  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:37.001  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:37.461  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:37.921  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 32ms, total 32ms
08-11 22:01:38.382  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:38.812  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 34ms, total 34ms
08-11 22:01:39.253  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 31ms, total 31ms
08-11 22:01:39.773  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 26ms, total 26ms
08-11 22:01:40.214  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2048K, 33% free 6491K/9680K, paused 35ms, total 35ms
08-11 22:01:40.644  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 28ms, total 28ms
08-11 22:01:41.135  20922-20922/? D/dalvikvm﹕ GC_FOR_ALLOC freed 2047K, 33% free 6491K/9680K, paused 21ms, total 21ms

任何有用的评论/回答都表示赞赏。

谢谢:)

3 个答案:

答案 0 :(得分:2)

看起来你并没有在这个循环中推进游标!像这样添加moveToNext()

Cursor cursor = db.rawQuery("SELECT idkliy, kliyentad FROM " + TABLE_KLIYENT, null);
cursor.moveToPosition(0);
while (!cursor.isAfterLast()) {
    linkedHashMap.put(cursor.getString(cursor.getColumnIndex(COLUMN_KLIYENTAD)), cursor.getInt(cursor.getColumnIndex(COLUMN_IDKLIY)));
    cursor.moveToNext();
}
cursor.close();

答案 1 :(得分:1)

试试这个:

SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT idkliy, kliyentad FROM " + TABLE_KLIYENT, null);
if(cursor.moveToFirst()){
    do{
        linkedHashMap.put(cursor.getString(cursor.getColumnIndex(COLUMN_KLIYENTAD)), cursor.getInt(cursor.getColumnIndex(COLUMN_IDKLIY)));
    }while (cursor.moveToNext());
}
cursor.close();
return  linkedHashMap;

请注意:

您很可能会收到错误。

您需要调整日志cat以在控制台中显示它们。

也许你有一个没有显示错误的过滤器。

答案 2 :(得分:1)

这是我看来的问题。

你有这个循环:

while (!cursor.isAfterLast()) {
    linkedHashMap.put(...);
}

但是你实际上从未改变过光标的位置,所以它永远不会退出while循环的条件,导致一个挂起的线程。

因为您在UI线程上执行此操作,该应用无法响应Android,因此看起来好像已经崩溃了。

在放置对象后尝试添加cursor.moveToNext()