在单击侦听器时查找viewById()
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I am writing an android program which has 4 buttons on the top placed horozontally and are horizontally scrollable. Below these buttons there is a list view. On click of any of the above buttons I want data to be displayed in the listview below.
Here is my activity:

 public class ListActivity extends Activity {
    /** Called when the activity is first created. */
    Context context=this;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       Button button1=(Button) findViewById(R.id.login);
       Button button2=(Button) findViewById(R.id.login1);
       Button button3=(Button) findViewById(R.id.login2);
       Button button4=(Button) findViewById(R.id.login3);
       button1.setOnClickListener(new OnClickListener(){
           @Override
           public void onClick(View v)
           {
               try{ListView listView = (ListView) findViewById(R.id.mylist);
                listView.setTextFilterEnabled(true);

                String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" ,"asdsad","asdsdsa","asdasdsa"};
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                        R.layout.list_item,values);

                    // Assign adapter to ListView
                    listView.setAdapter(adapter);}
               catch(Exception e){}
           }

       });



    }
}

Here is my xml file for main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"  
        >

        <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="thisisbutton1" />

        <Button 
            android:id="@+id/login1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="thisisbutton2"/>
                <Button 
            android:id="@+id/login2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="thisisbutton3"/>

                        <Button 
            android:id="@+id/login3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="thisisbutton4"/>
    </LinearLayout>        
        </HorizontalScrollView>



    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>

There are no errors(The listview worked fine when placed outside the onClickListener event). But I am getting the following errors on runtime.

 03-03 20:42:01.604: D/AndroidRuntime(401): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
03-03 20:42:01.604: D/AndroidRuntime(401): CheckJNI is ON
03-03 20:42:01.974: D/AndroidRuntime(401): --- registering native functions ---
03-03 20:42:02.744: D/ddm-heap(401): Got feature list request
03-03 20:42:03.494: D/PackageParser(60): Scanning package: /data/app/vmdl57341.tmp
03-03 20:42:03.844: D/dalvikvm(60): GC freed 6304 objects / 322096 bytes in 289ms
03-03 20:42:03.844: I/dalvikvm-heap(60): Grow heap (frag case) to 5.694MB for 16400-byte allocation
03-03 20:42:04.064: D/dalvikvm(60): GC freed 120 objects / 3784 bytes in 223ms
03-03 20:42:04.225: I/PackageManager(60): Removing non-system package:com.list
03-03 20:42:04.225: D/PackageManager(60): Removing package com.list
03-03 20:42:04.225: D/PackageManager(60):   Activities: com.list.ListActivity
03-03 20:42:04.384: D/PackageManager(60): Scanning package com.list
03-03 20:42:04.394: I/PackageManager(60): /data/app/vmdl57341.tmp changed; unpacking
03-03 20:42:04.414: D/installd(32): DexInv: --- BEGIN '/data/app/vmdl57341.tmp' ---
03-03 20:42:04.874: D/dalvikvm(407): DexOpt: load 67ms, verify 196ms, opt 4ms
03-03 20:42:04.894: D/installd(32): DexInv: --- END '/data/app/vmdl57341.tmp' (success) ---
03-03 20:42:04.894: D/PackageManager(60):   Activities: com.list.ListActivity
03-03 20:42:04.914: D/ActivityManager(60): Uninstalling process com.list
03-03 20:42:04.914: D/ActivityManager(60): Force removing process ProcessRecord{44e56498 370:com.list/10031} (com.list/10031)
03-03 20:42:04.914: I/Process(60): Sending signal. PID: 370 SIG: 9
03-03 20:42:04.985: D/ActivityManager(60): Received spurious death notification for thread android.os.BinderProxy@44e14da8
03-03 20:42:05.144: I/installd(32): move /data/dalvik-cache/data@app@vmdl57341.tmp@classes.dex -> /data/dalvik-cache/data@app@com.list.apk@classes.dex
03-03 20:42:05.155: D/PackageManager(60): New package installed in /data/app/com.list.apk
03-03 20:42:05.604: D/AndroidRuntime(401): Shutting down VM
03-03 20:42:05.624: D/dalvikvm(401): DestroyJavaVM waiting for non-daemon threads to exit
03-03 20:42:05.634: D/dalvikvm(401): DestroyJavaVM shutting VM down
03-03 20:42:05.644: D/dalvikvm(401): HeapWorker thread shutting down
03-03 20:42:05.644: D/dalvikvm(401): HeapWorker thread has shut down
03-03 20:42:05.644: D/jdwp(401): JDWP shutting down net...
03-03 20:42:05.644: I/dalvikvm(401): Debugger has detached; object registry had 1 entries
03-03 20:42:05.655: D/dalvikvm(401): VM cleaning up
03-03 20:42:05.674: D/dalvikvm(60): GC freed 3156 objects / 236576 bytes in 358ms
03-03 20:42:05.674: D/ActivityManager(60): Uninstalling process com.list
03-03 20:42:05.704: E/AndroidRuntime(401): ERROR: thread attach failed
03-03 20:42:05.734: D/dalvikvm(401): LinearAlloc 0x0 used 623916 of 5242880 (11%)
03-03 20:42:06.605: D/dalvikvm(60): GC freed 1211 objects / 76088 bytes in 453ms
03-03 20:42:06.714: W/ResourceType(60): Resources don't contain package for resource number 0x7f0700e5
03-03 20:42:06.725: W/ResourceType(60): Resources don't contain package for resource number 0x7f020031
03-03 20:42:06.725: W/ResourceType(60): Resources don't contain package for resource number 0x7f020030
03-03 20:42:06.725: W/ResourceType(60): Resources don't contain package for resource number 0x7f050000
03-03 20:42:06.914: W/ResourceType(60): Resources don't contain package for resource number 0x7f060000
03-03 20:42:06.964: W/ResourceType(60): Resources don't contain package for resource number 0x7f060001
03-03 20:42:07.255: D/dalvikvm(106): GC freed 96 objects / 3776 bytes in 586ms
03-03 20:42:07.604: W/ResourceType(60): Resources don't contain package for resource number 0x7f0700e5
03-03 20:42:07.614: W/ResourceType(60): Resources don't contain package for resource number 0x7f020031
03-03 20:42:07.614: W/ResourceType(60): Resources don't contain package for resource number 0x7f020030
03-03 20:42:07.614: W/ResourceType(60): Resources don't contain package for resource number 0x7f050000
03-03 20:42:07.654: W/ResourceType(60): Resources don't contain package for resource number 0x7f060000
03-03 20:42:07.664: W/ResourceType(60): Resources don't contain package for resource number 0x7f060001
03-03 20:42:08.165: D/AndroidRuntime(412): CheckJNI is ON
03-03 20:42:08.974: D/AndroidRuntime(412): --- registering native functions ---
03-03 20:42:09.764: D/ddm-heap(412): Got feature list request
03-03 20:42:10.425: I/ActivityManager(60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.list/.ListActivity }
03-03 20:42:10.545: I/ActivityManager(60): Start proc com.list for activity com.list/.ListActivity: pid=418 uid=10031 gids={}
03-03 20:42:10.555: D/AndroidRuntime(412): Shutting down VM
03-03 20:42:10.555: D/dalvikvm(412): DestroyJavaVM waiting for non-daemon threads to exit
03-03 20:42:10.564: D/dalvikvm(412): DestroyJavaVM shutting VM down
03-03 20:42:10.577: D/dalvikvm(412): HeapWorker thread shutting down
03-03 20:42:10.577: D/dalvikvm(412): HeapWorker thread has shut down
03-03 20:42:10.577: D/jdwp(412): JDWP shutting down net...
03-03 20:42:10.577: I/dalvikvm(412): Debugger has detached; object registry had 1 entries
03-03 20:42:10.577: D/dalvikvm(412): VM cleaning up
03-03 20:42:10.625: E/AndroidRuntime(412): ERROR: thread attach failed
03-03 20:42:10.684: D/dalvikvm(412): LinearAlloc 0x0 used 639500 of 5242880 (12%)
03-03 20:42:10.964: D/ddm-heap(418): Got feature list request
03-03 20:42:12.085: I/ActivityManager(60): Displayed activity com.list/.ListActivity: 1555 ms (total 1555 ms)
03-03 20:42:17.344: D/dalvikvm(230): GC freed 76 objects / 3256 bytes in 183ms
03-03 20:42:22.534: D/dalvikvm(106): GC freed 2394 objects / 139216 bytes in 289ms
03-03 20:43:32.094: D/dalvikvm(103): GC freed 11873 objects / 524240 bytes in 153ms

What can be the problem and how to rectify it?

#0

I will answer my own question. Actually the answer is not in the logic or code, it is in main.xml file(layout). just change this in horizontal scrollview of the above main.xml file.

android:layout_height="fill_parent"

to

android:layout_height="wrap_content"

It works fine. The List View is displayed on click og the first button.

推荐文章

jQuery动画

jQuery动画

推荐文章

为什么Haskell在扩展时会抱怨矛盾类型?

为什么Haskell在扩展时会抱怨矛盾类型?

推荐文章

如何将这个JS日历弹出窗口的输出更改为PHP的date('W')输出

如何将这个JS日历弹出窗口的输出更改为PHP的date('W')输出

推荐文章

Symfony2认证问题

Symfony2认证问题

推荐文章

用阴影画一条线,但只想留下阴影。国际标准化组织

用阴影画一条线,但只想留下阴影。国际标准化组织

推荐文章

是否可以将旧的MySQL数据文件夹移动到新的MySQL安装文件夹?

是否可以将旧的MySQL数据文件夹移动到新的MySQL安装文件夹?

推荐文章

在数据库中保存排名

在数据库中保存排名

推荐文章

如何在Apache2.3上安装DelphiWebServiceISAPI模块

如何在Apache2.3上安装DelphiWebServiceISAPI模块

推荐文章

休眠会话已消失

休眠会话已消失

推荐文章

SalesForce集成-自定义UI功能

SalesForce集成-自定义UI功能

推荐文章

如果使用session_id()设置过期会话,会发生什么情况?

如果使用session_id()设置过期会话,会发生什么情况?

推荐文章

如何将回调函数赋给调度员.BeginInvoke

如何将回调函数赋给调度员.BeginInvoke

推荐文章

如何计算股票自200年高点以来的周期

如何计算股票自200年高点以来的周期

推荐文章

带有join和count的mysql更新

带有join和count的mysql更新

推荐文章

直接在粉丝页面上跟踪类似Facebook的转换

直接在粉丝页面上跟踪类似Facebook的转换

推荐文章

切换菜单-单击子菜单-jQuery/HTML

切换菜单-单击子菜单-jQuery/HTML