0
當同一父視圖(列表視圖)內的Switch組件切換時,我試圖從視圖(TextView)
獲取值。當開關切換被調用的方法是:無法從Android開關組件的onClicked方法中獲取關聯的TextView值
public void onSwitchClicked(View list) {
Switch mySwitch = (Switch) list.findViewById(R.id.tgl_status);
// Is the toggle on?
boolean on = ((Switch) mySwitch).isChecked();
if (on) {
Toast.makeText(getApplicationContext(), "TURN ON: Request sent.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "TURN OFF: Request sent.", Toast.LENGTH_LONG).show();
}
}
這多少代碼工作正常,但是當我嘗試檢索組件的一個TextView值,它是存在於同一個列表視圖,程序崩潰。這裏是我正在試圖做到這一點(我把onSwitchClicked()
方法體中的代碼):
TextView tV = (TextView) list.findViewById(R.id.pid);
String pid = tV.getText().toString();
考慮列表視圖的XML:
<?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="65dip"
android:orientation="vertical"
android:layout_centerVertical="true"
>
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid" <!-- **this is the value I want** ... -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<TextView
android:id="@+id/statusT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:textSize="17dip"
android:layout_below="@+id/name"
/>
<Switch <!-- **when this Switch is toggled** -->
android:id="@+id/tgl_status"
android:layout_alignParentRight="true"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:layout_centerVertical="true"
android:textOff="OFF"
android:textOn="ON"
android:onClick="onSwitchClicked"
/>
</RelativeLayout>
我想pid
的TextView的價值被分配到pid
字符串。我怎樣才能做到這一點?
這裏的logcat的:
W/dalvikvm(7749): threadid=1: thread exiting with uncaught exception (group=0x41560ba8)
E/AndroidRuntime(7749): FATAL EXCEPTION: main
E/AndroidRuntime(7749): Process: com.iotautomationtech.androidapp, PID: 7749
E/AndroidRuntime(7749): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime(7749): at android.view.View$1.onClick(View.java:3823)
E/AndroidRuntime(7749): at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(7749): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
E/AndroidRuntime(7749): at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(7749): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(7749): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(7749): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(7749): at android.app.ActivityThread.main(ActivityThread.java:5001)
E/AndroidRuntime(7749): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(7749): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(7749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(7749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(7749): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(7749): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(7749): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(7749): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(7749): at android.view.View$1.onClick(View.java:3818)
E/AndroidRuntime(7749): ... 12 more
E/AndroidRuntime(7749): Caused by: java.lang.NullPointerException
E/AndroidRuntime(7749): at com.iotautomationtech.androidapp.AllDevicesActivity.onSwitchClicked(AllDevicesActivity.java:100)
E/AndroidRuntime(7749): ... 15 more
一些幫助?
它的工作,謝謝。 – 2014-09-06 18:42:06