我正在开发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>
logcat的
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!
答案 0 :(得分: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 中,但您已将其声明为一边,因此您的listview始终为null,您将无法获取视图,直到您设置内容视图。
在oncreate中设置contentView后放置该代码