更新时间:2022-12-26 18:53:12
如果你的活动
是扩展 ListActivity
那么类确保您的主要布局文件必须包含一个的ListView
元素如下。
< ListView的机器人:ID =@机器人:ID /列表机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent/>
您的应用程序崩溃,因为你是一个设置适配器的ListView这确实是不是在你的布局文件存在。
查看文档:http://developer.android.com/reference/android/app/ListActivity.html
Well, I've got an application that crashes on load up because of a setContentView line. Now before you ask, its when I comment out the setContentView line on line 15 or so that I get a working app. But the moment I place it in, it errors. Now this is using 2.1-update1 android.
Java:
package com.clark.listview;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class listview extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone", "ClarkPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,
R.id.label, names));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
if (keyword.startsWith("Windows7") || keyword.startsWith("iPhone")
|| keyword.startsWith("Solaris")) {
launchTest();
}
}
protected void launchTest() {
Intent i = new Intent(this, home.class);
startActivity(i);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:cacheColorHint="#00000000"
android:background="@drawable/bg">
</LinearLayout>
If your Activity
is extending the ListActivity
class then ensure that your main layout file must contain a ListView
element as given below.
<ListView android:id="@android:id/list" android:layout_height="wrap_content"
android:layout_width="match_parent" />
your application is crashing because you are setting an adapter to ListView which is indeed not exist in your layout file.
See the documentation : http://developer.android.com/reference/android/app/ListActivity.html