我是java的新手。我已经创建了一个listview,并将我的包命名为AndroidList,并尝试在模拟器上查看它是如何工作的,但它会显示错误“不幸的是Androidlist已停止”。
这是.java代码
package com.exercise.androidlist;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class AndroidList extends ListActivity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, DayOfWeek));
}
}
这是logcat
02-13 21:24:02.330: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.330: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.390: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.390: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.500: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.531: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.531: I/Choreographer(409): Skipped 35 frames! The application may be doing too much work on its main thread.
02-13 21:24:02.600: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.600: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.693: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.693: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:02.720: I/Choreographer(409): Skipped 44 frames! The application may be doing too much work on its main thread.
02-13 21:24:02.812: E/StrictMode(628): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cda940 that was originally bound here
02-13 21:24:02.812: E/StrictMode(628): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
02-13 21:24:02.812: E/StrictMode(628): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
02-13 21:24:02.812: E/StrictMode(628): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
02-13 21:24:02.812: E/StrictMode(628): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
02-13 21:24:02.812: E/StrictMode(628): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
02-13 21:24:02.812: E/StrictMode(628): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
02-13 21:24:02.812: E/StrictMode(628): at
02-13 21:24:05.291: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:05.291: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:05.380: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:05.380: W/Trace(409): Unexpected value from nativeGetEnabledTags: 0
02-13 21:24:05.411: I/Choreographer(409): Skipped 39 frames! The application may be doing too much work on its main thread.
答案 0 :(得分:1)
setContentView(R.layout.main);
对于加载活动布局非常重要= P
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, DayOfWeek));
}
并且LogCat
的消息与查找原因无关,请查找“例外”一词:)
答案 1 :(得分:0)
可能是因为应用程序没有加载任何视图?
取消注释//setContentView(R.layout.main);
然后重试。
答案 2 :(得分:0)
使用Listview
创建ListActivity
时,两件事必须包含一个ListView
,android:id
属性设置为@android:id / list(NOT android:id = “@ + id / list”)。
如果您定义ListView
并将android:id
属性设置为
@ + id / list然后它会给出错误。
public class AndroidList extends ListActivity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, DayOfWeek);
// Assign adapter to List
setListAdapter(adapter);
}
}
适配器用于向ListView
提供数据
参数:
simple_list_item_1
:Android内部布局视图
values
:用户定义的数据数组。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, DayOfWeek);
// Assign adapter to List
setListAdapter(adapter);