onClickListener中的Findviewbyid()

时间:2012-03-03 15:22:13

标签: android android-listview

我正在编写一个android程序,它顶部有4个按钮,可以水平滚动。在这些按钮下面有一个列表视图。点击上述任何按钮,我希望数据显示在下面的列表视图中 这是我的活动:

 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){}
           }

       });



    }
}

这是main.xml的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>

没有错误(列表视图放在onClickListener事件之外时工作正常)。但是我在运行时遇到以下错误。

 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

可能是什么问题以及如何纠正它?

1 个答案:

答案 0 :(得分:0)

我会回答我自己的问题。实际上答案不在逻辑或代码中,而是在main.xml文件(布局)中。只需在上面的main.xml文件的水平滚动视图中更改它。

android:layout_height="fill_parent"

android:layout_height="wrap_content"

工作正常。单击og第一个按钮时会显示列表视图。