我在调试时测试我的应用程序并在此行崩溃:
listAdapter = new ArrayAdapter<String>(this, R.layout.cd_edittext_for_listview, planetList);
出现此错误:系统服务在 onCreate() 之前对活动不可用
我用这段代码解决了这个错误(用上下文改变它): listAdapter = new ArrayAdapter(promptsView.getContext(), R.layout.cd_edittext_for_listview, planetList); 并使用“EditText”更改 XML 文件“EditView”!*
我对项目“editText_for_listview.xml”有这个布局:
<EditView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowEditView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</EditView>
而我的主要布局“items_listview.xml”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mylistview"
android:layout_gravity="center" />
</LinearLayout>
代码是这样的:
public void _ShowItems(String sTitle,android.content.Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(sTitle);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final View promptsView = inflater.inflate(R.layout.items_listview, null);
ListView mainListView ;
ArrayAdapter<String> listAdapter ;
mainListView = (ListView) promptsView.findViewById( R.id.editText_for_listview );
String[] planets = new String[] { "Test1", "Test2", "Test3" };
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
listAdapter = new ArrayAdapter<String>(this, R.layout.cd_edittext_for_listview, planetList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
builder.setView(promptsView)
.setPositiveButton("One", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Two", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
你能帮助我吗?我哪里错了?
谢谢