在Android中解雇TableRow事件需要帮助?

时间:2010-11-10 09:59:17

标签: android

海友,           我想在用户单击UI中的特定表行时调用或触发表行的onclick事件。 这是我的xml代码

  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/table_bars" android:layout_width="wrap_content" android:layout_height="fill_parent"android:background="@drawable/corner" android:stretchColumns="1" android:layout_margin="10dp">
  <TableRow>
  <TextView android:id="@+id/barsp_baradd_value"
  android:textSize="15sp" android:padding="10dp" android:layout_width="wrap_content" />
   </TableRow>
   <View android:layout_height="1dp" android:background="#FF909090" />
   <TableRow>
   <TextView android:id="@+id/barsp_barphone_value"
             android:textSize="15sp"    android:padding="10dp" android:layout_width="wrap_content" />
    <ImageView android:id="@+id/san_arrow2" android:src="@drawable/newforward" android:layout_alignParentRight="true"
android:adjustViewBounds="true" android:layout_marginTop="10dp"
android:layout_width="30dp"  android:layout_height="35dp" android:paddingRight="4dp" />
 </TableRow>
  <View android:layout_height="1dp" android:background="#FF909090" />
  <TableRow android:id="@+id/barsite">
   <TextView android:id="@+id/barsp_barsite_value"
  android:textSize="15sp" android:padding="10dp" android:layout_width="wrap_content" />
<ImageView android:id="@+id/san_arrow3" android:src="@drawable/newforward" android:layout_alignParentRight="true"
android:adjustViewBounds="true" android:layout_marginTop="10dp"
android:layout_width="30dp"  android:layout_height="35dp" android:paddingRight="4dp" />
                  </TableRow>

                </TableLayout>

我的Java代码是

TableRow row = (TableRow)findViewById(R.id.row1);

                row.setOnClickListener(new OnClickListener() {



                                @Override
                 public void onClick(View v) {

                                // TODO Auto-generated method stub

                                try {

                                        //Log.v(TAG, "try");

                                        uri = Uri.parse("geo:0,0?q=" + VALUE_baraddress1[0] + ", "

                                                        + VALUE_baraddress2[0] + ", "

                                                        + VALUE_baraddress3[0]);

                                        //Log.v(TAG, VALUE_baraddress1[0] + ", "

                                                //      + VALUE_baraddress2[0] + ", "

                                                //      + VALUE_baraddress3[0]);

                                       startActivity(new Intent(Intent.ACTION_VIEW, uri));

                                } catch (Exception e) {

                                        //Log.v(TAG, "catch");

                                        uri = Uri.parse("geo:0,0?q=" + VALUE_baraddress1[0] + ", "

                                                        + VALUE_baraddress3[0]);

                                        //Log.v(TAG, VALUE_baraddress1[0] + ", "

                                        //              + VALUE_baraddress3[0]);

                                        startActivity(new Intent(Intent.ACTION_VIEW, uri));



                                }



                        }

                });



i had got null pointer exception in this line

TableRow row = (TableRow)table.getChildAt(0);

Logcat消息

11-10 15:47:04.164: ERROR/AndroidRuntime(12720):     ... 11 more
11-10 15:47:04.164: ERROR/AndroidRuntime(12720): Caused by: java.lang.NullPointerException
11-10 15:47:04.164: ERROR/AndroidRuntime(12720):     at com.fsp.blacksheep.BS_Bars.onCreate(BS_Bars.java:546)
11-10 15:47:04.164: ERROR/AndroidRuntime(12720):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
11-10 15:47:04.164: ERROR/AndroidRuntime(12720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
11-10 15:47:04.164: ERROR/AndroidRuntime(12720):     ... 19 more
11-10 15:47:06.324: ERROR/JavaBinder(972): !!! FAILED BINDER TRANSACTION !!!

1 个答案:

答案 0 :(得分:3)

我不太确定这是否有效,但如果您计划android:clickable可点击,则应将true属性设置为TableRows。我没试过。