我希望在选中复选框后更改文本字段的上下文。
TextView和Button位于片段内。
的TextView:
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView_test" />
按钮:
<CheckBox android:id="@+id/checkBox_Alz"
android:text="Alzheimer's"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onCheckBoxClicked" />
的活动:
public void showConditionFragment() {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_content, new ConditionFragment());
fragmentTransaction.commit();
}
public static class ConditionFragment extends Fragment {
private final String LOG = this.getClass().getSimpleName();
public ConditionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.condition_choice, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
监听器:
public void onCheckBoxClicked(View view) {
TextView viewTest = (TextView)findViewById(R.id.textView_test);
viewTest.setText("test");
}
一旦我选择了一个方框,我就会收到一条弹出错误,说该应用无效。如果侦听器方法没有内部代码,则选择该框没有任何错误。
编辑:
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phorloop.graymatters, PID: 14416
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4725)
at android.view.View.performClick(View.java:5637)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
at android.view.View.performClick(View.java:5637)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to com.rey.material.widget.TextView
at com.phorloop.graymatters.view.activity.WelcomeActivity.onCheckBoxClicked(WelcomeActivity.java:180)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
at android.view.View.performClick(View.java:5637)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
您似乎导入了错误的TextView类活动(com.rey.material.widget.TextView)。
尝试使用
导入android.support.v7.widget.AppCompatTextView
public void onCheckBoxClicked(View view) {
AppCompatTextView viewTest = (AppCompatTextView)findViewById(R.id.textView_test);
viewTest.setText("test");
}