Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 标题不工作的ListView_Android_Android Listview - Fatal编程技术网

Android 标题不工作的ListView

Android 标题不工作的ListView,android,android-listview,Android,Android Listview,我得到了一个列表视图,它工作得很好,但我希望我的标题在顶部,所以我做了一个新的布局,在那里我放置了标题和列表视图。现在我接近一个力,我真的不知道它有什么问题 public class AndroidListViewActivity extends ListActivity { String value; // Places Listview ListView lv; @Override public void onCreate(Bundle sa

我得到了一个
列表视图
,它工作得很好,但我希望我的标题在顶部,所以我做了一个新的布局,在那里我放置了标题和
列表视图
。现在我接近一个力,我真的不知道它有什么问题

public class AndroidListViewActivity extends ListActivity {

    String value;
    // Places Listview
        ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_item3);

        String[] restaurant = getResources().getStringArray(R.array.restaurant);
        String[] bar = getResources().getStringArray(R.array.bar);
        String[] gas = getResources().getStringArray(R.array.gas);
        String[] help = getResources().getStringArray(R.array.help);
        String[] hotel = getResources().getStringArray(R.array.hotel);
        String[] shopping = getResources().getStringArray(R.array.shopping);
        String[] bank = getResources().getStringArray(R.array.bank);
        String[] bus = getResources().getStringArray(R.array.bus);
        String[] film = getResources().getStringArray(R.array.film);


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
         value = extras.getString("categoriename");
        }

        // storing string resources into Array
        if(value.equals("restaurant")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, restaurant));
        }if(value.equals("bar")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bar));
        }if(value.equals("gas")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, gas));
        }if(value.equals("film")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, film));
        }if(value.equals("help")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, help));
        }if(value.equals("bus")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bus));
        }if(value.equals("bank")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bank));
        }if(value.equals("shopping")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, shopping));
        }if(value.equals("hotel")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, hotel));
        }





        lv = (ListView) findViewById(R.id.list3);

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              // selected item
              String product = ((TextView) view).getText().toString();

              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), MainActivity.class);
              // sending data to new activity
              i.putExtra("thetext", product);
              startActivity(i);

          }
        });
    }

ListActivity
有一个特殊的行为,您在代码中没有考虑到它。如果使用
R.layout.list_item3
布局文件作为
ListActivity
的内容视图,则该布局文件中的
ListView
必须具有id
android:id=“@android:id/list”
(因此
ListActivity
将知道从自定义布局使用哪个
ListView
小部件). 因此,在
R.layout.list_item3
中修改
ListView
项如下:

//....
 <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2" />
// ...

嗨,对不起,我把日志放上去了。嗨,已经谢谢你了。我改了,但我还是很接近。我把圆木架起来。你能看一下吗。提前感谢。@user1827512错误在您的日志猫中非常清楚,“原因:java.lang.RuntimeException:您的内容必须有一个id属性为“android.R.id.list”的ListView”,请再次检查您的XML。卢卡申奥是绝对正确的。是的,我看到了。但我确实改变了。。。但是我有另一个活动,它也有一个listview,并且它也使用id:list。。可能是这样吗?@user1827512
AndroidListViewActivity
活动正在引发异常。在再次运行项目之前,请尝试清理项目。进入菜单项目->清洁
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF"
    android:id="@+id/mainLayout"
    > 
     <ImageView
        android:id="@+id/imageButton2"
        android:layout_width="fill_parent"
        android:layout_height="52dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/header"
        android:src="@drawable/list_header" />
    <ImageButton
        android:id="@+id/imageButtonSearch"
        android:layout_width="60dip"
        android:layout_height="45dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/header"
        android:src="@drawable/search" 
        android:background="@android:color/transparent" 
        />
    <ImageView
        android:id="@+id/imageButton3"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/titel" />

    <ListView
        android:id="@+id/list3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2" />

    </RelativeLayout>
    12-04 18:29:49.886: E/AndroidRuntime(371): FATAL EXCEPTION: main
12-04 18:29:49.886: E/AndroidRuntime(371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laurenswuyts.find.it/com.laurenswuyts.find.it.AndroidListViewActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.os.Looper.loop(Looper.java:130)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 18:29:49.886: E/AndroidRuntime(371):  at java.lang.reflect.Method.invokeNative(Native Method)
12-04 18:29:49.886: E/AndroidRuntime(371):  at java.lang.reflect.Method.invoke(Method.java:507)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 18:29:49.886: E/AndroidRuntime(371):  at dalvik.system.NativeStart.main(Native Method)
12-04 18:29:49.886: E/AndroidRuntime(371): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.Activity.setContentView(Activity.java:1657)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.laurenswuyts.find.it.AndroidListViewActivity.onCreate(AndroidListViewActivity.java:23)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 18:29:49.886: E/AndroidRuntime(371):  ... 11 more
//....
 <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2" />
// ...
lv = getListView();