简体   繁体   中英

Android app crashing when implementing button listener

I'm looking to change the context of a text field when a check box has been selected.

TextView and Button are within a fragment.

TextView:

<TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView_test" />

Button:

<CheckBox android:id="@+id/checkBox_Alz"
            android:text="Alzheimer's"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onCheckBoxClicked" />

Activity:

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();
    }

}

Listener:

public void onCheckBoxClicked(View view) {
    TextView viewTest = (TextView)findViewById(R.id.textView_test);
    viewTest.setText("test");
}

Once I selected a box I am getting a pop up error saying the app is not working. If the listener method is left with no code inside then the box is selected without any errors.

EDIT:

Error Log:

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) 

Looks like you are importing wrong TextView class activity(com.rey.material.widget.TextView).

Try to import android.support.v7.widget.AppCompatTextView using

public void onCheckBoxClicked(View view) {
    AppCompatTextView viewTest = (AppCompatTextView)findViewById(R.id.textView_test);
    viewTest.setText("test");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM