【问题标题】:adding button listener to viewpager crashes app [duplicate]将按钮侦听器添加到 viewpager 崩溃应用程序 [重复]
【发布时间】:2018-10-13 11:14:10
【问题描述】:

我创建了一个有 3 页的 viewpager。目前其中一个有一个按钮,但是当我尝试向按钮添加按钮侦听器时,应用程序崩溃但调试不报告任何错误。除了断开与虚拟机的连接。 最初视图被夸大了,并且在 case 语句本身中添加了侦听器,但这也不起作用。

如果 Android 出现问题,控制台不应该说些什么吗?

片段及其行为如下 应用程序在 setOnClickListener 上崩溃。 有任何想法吗? 谢谢

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/app_name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1" />

    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintVertical_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView;
            View mainView = inflater.inflate(R.layout.fragment_main, container, false);
            button= (Button) getActivity().findViewById(R.id.imageButton2);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Code here executes on main thread after user presses button
                    TextView textView = (TextView)getActivity().findViewById(R.id.section_label);
                    textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                }
            });

            int page=getArguments().getInt(ARG_SECTION_NUMBER);
            switch (page)
            {
                case 1:
                    rootView=mainView;

                    break;
                case 2:
                    rootView = inflater.inflate(R.layout.activity_options, container, false);
                    break;
                case 3:
                    rootView = inflater.inflate(R.layout.activity_request, container, false);
                    break;
                    default:
                        rootView = inflater.inflate(R.layout.fragment_main, container, false);
                        break;

            }


            return rootView;
        }

【问题讨论】:

  • adding button listener to viewpager crashes app 您的 crash-log 在哪里,您需要与问题分享 crash-log
  • button= (Button) mainView.findViewById(R.id.imageButton2);

标签: android button android-viewpager


【解决方案1】:
  1. 错误findViewById

试试这个

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView;
            View mainView= inflater.inflate(R.layout.fragment_main, container, false);
          ImageButton  button= (ImageButton) mainView.findViewById(R.id.imageButton2);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Code here executes on main thread after user presses button
                    TextView textView = (TextView)mainView.findViewById(R.id.section_label);
                    textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                }
            });

            int page=getArguments().getInt(ARG_SECTION_NUMBER);
            switch (page)
            {
                case 1:
                    rootView=mainView;

                    break;
                case 2:
                    rootView = inflater.inflate(R.layout.activity_options, container, false);
                    break;
                case 3:
                    rootView = inflater.inflate(R.layout.activity_request, container, false);
                    break;
                    default:
                        rootView = inflater.inflate(R.layout.fragment_main, container, false);
                        break;

            }


            return rootView;
        }

【讨论】:

    【解决方案2】:

    出错的情况可能不止一种,我认为其中一种情况是 ImageButton 无法转换为 Button。如果不是这种情况,请尝试使用调试器查看 null(很可能是 null)

    【讨论】:

      【解决方案3】:

      更改以下内容

      button= (Button) getActivity().findViewById(R.id.imageButton2); 
      

      button= (Button) mainView.findViewById(R.id.imageButton2);
      

      并在膨胀视图后初始化您的button。因为它可能不会在初始化时产生问题,但如果您的视图更改实际按钮的位置,则会崩溃。

      【讨论】:

        【解决方案4】:

        感谢上面的答案,解决方案是更改一行 从 button= (Button) getActivity().findViewById(R.id.imageButton2); 到

        ImageButton 按钮= mainView.findViewById(R.id.imageButton2);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-27
          • 1970-01-01
          • 1970-01-01
          • 2018-10-26
          • 1970-01-01
          • 2017-12-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多