-1

我正在使用eclipse开发一个android应用程序,我正在测试一些代码以查看它是如何工作的,但是有一些错误我试图解决它们但我不能。我是移动开发的新手,我必须制定一个待办事项清单。问题是我不熟悉适配器和 listView,所以我试图理解它们。

Java 代码

     package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SimpleListItem1Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_list_item1);
        ListView listView = (ListView) findViewById(R.id.mylist);  

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

             listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.simple_list_item1, menu);
        return true;  }

    ListView listView = (ListView) findViewById(R.id.mylist);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2" };

    // Define a new Adapter
    // First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data

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



}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout> 

日志猫

 03-14 14:57:07.643: E/AndroidRuntime(773):     ... 11 more
[2013-03-14 17:54:54 - test] ------------------------------
[2013-03-14 17:54:54 - test] Android Launch!
[2013-03-14 17:54:54 - test] adb is running normally.
[2013-03-14 17:54:54 - test] Performing com.example.test.SimpleListItem1Activity activity launch
[2013-03-14 17:54:54 - test] Automatic Target Mode: launching new emulator with compatible AVD 'AVD_for_Nexus_S_by_Google'
[2013-03-14 17:54:54 - test] Launching a new emulator with Virtual Device 'AVD_for_Nexus_S_by_Google'
[2013-03-14 17:55:08 - Emulator] emulator: warning: opening audio input failed
[2013-03-14 17:55:08 - Emulator] 
[2013-03-14 17:55:08 - test] New emulator found: emulator-5554
[2013-03-14 17:55:08 - test] Waiting for HOME ('android.process.acore') to be launched...
[2013-03-14 17:56:06 - test] HOME is up on device 'emulator-5554'
[2013-03-14 17:56:06 - test] Uploading test.apk onto device 'emulator-5554'
[2013-03-14 17:56:07 - test] Installing test.apk...
[2013-03-14 17:57:06 - test] Success!
[2013-03-14 17:57:06 - test] Starting activity com.example.test.SimpleListItem1Activity on device emulator-5554
[2013-03-14 17:57:09 - test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.SimpleListItem1Activity }
[2013-03-14 18:07:20 - test] ------------------------------
[2013-03-14 18:07:20 - test] Android Launch!
[2013-03-14 18:07:20 - test] adb is running normally.
[2013-03-14 18:07:20 - test] Performing com.example.test.SimpleListItem1Activity activity launch
[2013-03-14 18:07:20 - test] Automatic Target Mode: launching new emulator with compatible AVD 'AVD_for_Nexus_S_by_Google'
[2013-03-14 18:07:20 - test] Launching a new emulator with Virtual Device 'AVD_for_Nexus_S_by_Google'
[2013-03-14 18:07:23 - Emulator] emulator: warning: opening audio input failed
[2013-03-14 18:07:23 - Emulator] 
[2013-03-14 18:07:23 - test] New emulator found: emulator-5554
[2013-03-14 18:07:23 - test] Waiting for HOME ('android.process.acore') to be launched...
[2013-03-14 18:08:21 - test] HOME is up on device 'emulator-5554'
[2013-03-14 18:08:21 - test] Uploading test.apk onto device 'emulator-5554'
[2013-03-14 18:08:21 - test] Installing test.apk...
[2013-03-14 18:10:25 - test] Failed to install test.apk on device 'emulator-5554!
[2013-03-14 18:10:25 - test] (null)
[2013-03-14 18:10:25 - test] Launch canceled!
4

1 回答 1

2

编辑:

应该是这样的

public class SimpleListItem1Activity extends Activity {
 String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_list_item1);
        ListView listView = (ListView) findViewById(R.id.mylist);  

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
             listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.simple_list_item1, menu);
        return true; 
 }

}

     ListView listView = (ListView) findViewById(R.id.mylist);  

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

       listView.setAdapter(adapter);

这段代码应该在 onCreate 里面,但是你已经在外面声明了,所以你的列表视图总是空的,在你设置内容视图之前你不会得到视图。

在 oncreate 中设置 contentView 后放置该代码

于 2013-03-14T14:49:09.633 回答