执行完所有代码后应用程序崩溃

时间:2014-03-04 12:13:54

标签: android json android-listview android-arrayadapter

我制作程序,解析JSON文本并输出到ListView,但是在执行完所有代码后应用程序崩溃,为什么?

 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Make app");

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        ListView lv = (ListView) findViewById(R.id.listView);
        ArrayList<String> listIdJson = idJsonList();  //get ArrayList          

        Log.d(TAG, "oN1");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    R.layout.activity_main, listIdJson);
        lv.setAdapter(adapter);

        Log.d(TAG, "oN exit");

    }

日志输出:

03-04 06:30:50.056    1484-1484/ru.vlad.pbj D/myLogs﹕ Make app
03-04 06:30:51.826    1484-1484/ru.vlad.pbj D/myLogs﹕ oN1
03-04 06:30:51.826    1484-1484/ru.vlad.pbj D/myLogs﹕ oN exit

3 个答案:

答案 0 :(得分:2)

您将相同的布局传递给:

setContentView(R.layout.activity_main);  

而且,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    R.layout.activity_main, listIdJson);

答案 1 :(得分:2)

更改此

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.activity_main, listIdJson);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listIdJson);

有关详细信息,请访问:http://www.vogella.com/tutorials/AndroidListView/article.html

答案 2 :(得分:1)

您无法为Array Adapter设置内容视图,您可以按如下方式更改它。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listIdJson);