android - 单击切换按钮时 TextInputLayout (TextInputEditText) NullpointerException

标签 android nullpointerexception android-design-library android-textinputlayout

我在密码字段中使用 TextInputLayout(和 TextInputEditText),但当我按下平板电脑上的切换按钮时,我的应用程序出现故障。

我在其他设备上进行了测试,该应用运行得非常棒 这里有一些信息:

致力于
- 连结 5 - 6.0.1
- Positivo S440 - 4.4.2
- 三星 Galaxy Tab SM-P550 - 6.0.1

崩溃于
- 平板电脑:三星 SM-T530
- 安卓:5.0.2
- Android 支持设计版本:25.3.1

我的 XML


android.support.design.widget.TextInputLayout
    android:id="@+id/til_password"
    style="@style/Text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:hint="@string/lbl_password"
    app:passwordToggleEnabled="true"
    app:passwordToggleTint="@color/theme_accent"

    android.support.design.widget.TextInputEditText
        android:id="@+id/inp_password"
        style="@style/Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:imeOptions="actionDone"
        android:inputType="textPassword"
        android:textColor="@color/theme_accent"
        android:textSize="@dimen/text_size_subhead"

android.support.design.widget.TextInputLayout

My code


public class SyncFragment extends GenericFragment implements TaskCallback,
        View.OnClickListener, WifiAdapter.WifiListener, BackIntercepter, TextView.OnEditorActionListener {

    <b>private TextInputEditText mInpPassword;</b>

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.layout_sync, container, false);
        ButterKnife.bind(this, view);
        <b>findViews(view);</b>

        if (getArguments() != null) {
            List wifis = getArguments().getParcelableArrayList(Extras.WIFIS);
            if(wifis != null) {
                Log.d("HACL.TEST", "content extra info: " + Extras.WIFIS);
                Log.d("HACL.TEST", "content wifis: " + wifis.toString());
                updateSpinner(wifis);
                networkId = getArguments().getInt(Extras.NETWORK_ID);
                airConWifiSSID = getArguments().getString(Extras.AIR_CON_WIFI);
            }
        }

        <b>setListeners();</b>
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        try {
            // If return from SyncingFragment, load which Wi-Fi network was selected and show
            // credential screen instead of Wi-Fi list screen
            if(getArguments().getBoolean(Extras.BACK_FROM_SYNCING)) {
                if (getArguments().getParcelable(Extras.WIFI) != null) {
                    Wifi wifi = getArguments().getParcelable(Extras.WIFI);
                    mOutNetwork.setText(wifi.getSsid());
                    mOutNetwork.setTextColor(getResources().getColor(R.color.theme_accent));
                    mOutNetwork.setAlpha(1);
                    mOutNetwork.setTag(wifi);
                    Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/FonteX-Regular.ttf");
                    mOutNetwork.setTypeface(face);
                    <b>mInpPassword.setTypeface(face);</b>
                }
                showCredentials();
            }
            else {
                showList();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void findViews(View view) {
        Typeface face = Typeface.createFromAsset(getContext().getAssets(),"fonts/FontX-Regular.ttf");

        <b>mInpPassword = (TextInputEditText) view.findViewById(R.id.inp_password);
        mInpPassword.setTypeface(face);</b>

        .....
    }

    private void finishSync() {

        if (mOutNetwork.getTag() == null) {
            Toast.makeText(getActivity(), R.string.msg_select_an_network, Toast.LENGTH_SHORT).show();
            return;
        }

        <b>String password = mInpPassword.getText().toString();</b>
        if (verifyPassword(password)) {
            Wifi wifi = (Wifi) mOutNetwork.getTag();
            wifi.setPassphrase(password);

            Bundle bundle = new Bundle();
            bundle.putParcelable(Extras.WIFI, wifi);
            bundle.putParcelableArrayList(Extras.WIFIS, new ArrayList(wifiOnSpinner));
            bundle.putInt(Extras.NETWORK_ID, networkId);
            bundle.putString(Extras.AIR_CON_WIFI, airConWifiSSID);
            getGenericActivity().openMainFragment(SyncingFragment.class, bundle);
        } else {
            Toast.makeText(getActivity(), R.string.msg_password_invalid, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.equals(mLyNetwork)) {
            try {
                CommonUtils.hideInputKeyboard(getActivity());
            } catch (RuntimeException ignored) {

            }
            showList();
        }

        if (v.equals(mBtnSync)) {
            <b>finishSync();</b>
        }
    }

    @Override
    public void onWifiSelected(Wifi wifi) {
        mOutNetwork.setText(wifi.getSsid());
        mOutNetwork.setTextColor(getResources().getColor(R.color.theme_accent));
        mOutNetwork.setAlpha(1);
        mOutNetwork.setTag(wifi);
        Typeface face = Typeface.createFromAsset(getContext().getAssets(),"fonts/ElectroluxSans-Regular.ttf");
        mOutNetwork.setTypeface(face);
        mInpPassword.setTypeface(face);

        if (wifi.getSecurityType() > 0) {
            <b>showCredentials();</b>
        } else {
            <b>finishSync();</b>
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        Wifi tag = (Wifi) mOutNetwork.getTag();
        // If Wi-Fi network has some security type, minimum password length is:
        // For a 64 bit WEP network the password is 5-characters.
        // For a 128 bit WEP network the password is 13-characters.
        // WPA uses either a passphrase (a shared secret) that is comprised of 8 to 63 characters
        if (textView.getText() != null
                && textView.getText().length()  0) {
            mBtnSync.setEnabled(false);
        } else {
            mBtnSync.setEnabled(true);
        }

        if (i == EditorInfo.IME_ACTION_DONE && mBtnSync.isEnabled()) {
            <b>finishSync();</b>
            return true;
        }
        return false;
    }
    public void showCredentials() {
        int duration = getResources().getInteger(android.R.integer.config_mediumAnimTime);
        lyCredentials.setVisibility(View.VISIBLE);
        ViewCompat.animate(listNetwork).alpha(0f)
                .setInterpolator(new DecelerateInterpolator())
                .setDuration(duration)
                .setListener(new ViewPropertyAnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(View view) {
                        listNetwork.setVisibility(View.GONE);
                    }

                })
                .start();
        ViewCompat.animate(lyCredentials).alpha(1f)
                .setStartDelay(duration)
                .setInterpolator(new DecelerateInterpolator())
                .setListener(new ViewPropertyAnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(View view) {
                        <b>mInpPassword.requestFocus();
                        CommonUtils.showInputKeyboard(getActivity(), mInpPassword);</b>
                    }

                })
                .setDuration(duration)
                .start();

        mBtnSync.setEnabled(false);
    }

    @Override
    public void onWifiSelected(Wifi wifi) {
        mOutNetwork.setText(wifi.getSsid());
        mOutNetwork.setTextColor(getResources().getColor(R.color.theme_accent));
        mOutNetwork.setAlpha(1);
        mOutNetwork.setTag(wifi);
        Typeface face = Typeface.createFromAsset(getContext().getAssets(),"fonts/FontX-Regular.ttf");
        mOutNetwork.setTypeface(face);
        <b>mInpPassword.setTypeface(face);</b>

        if (wifi.getSecurityType() > 0) {
            <b>showCredentials();</b>
        } else {
            finishSync();
        }
    }
    private void setListeners() {
        mLyNetwork.setOnClickListener(this);
        mInpPassword.setOnEditorActionListener(this);
        mBtnSync.setOnClickListener(this);
    }
}

这是日志


05-15 09:22:31.199 20164-20164/com.xxxxxxx.yyyyyyyy.zzzz E/AndroidRuntime: FATAL EXCEPTION: 
main
Process: com.xxxxxxxx.yyyyyyyyy.zzzzz, PID: 20164
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.graphics.drawable.AnimatedVectorDrawable.start(AnimatedVectorDrawable.java:423)
at android.graphics.drawable.AnimatedStateListDrawable$AnimatedVectorDrawableTransition.start(AnimatedStateListDrawable.java:322)
at android.graphics.drawable.AnimatedStateListDrawable.selectTransition(AnimatedStateListDrawable.java:226)
at android.graphics.drawable.AnimatedStateListDrawable.onStateChange(AnimatedStateListDrawable.java:153)
at android.graphics.drawable.Drawable.setState(Drawable.java:644)
at android.support.v4.graphics.drawable.DrawableWrapperGingerbread.setState(DrawableWrapperGingerbread.java:145)
at android.support.v4.graphics.drawable.DrawableWrapperLollipop.setState(DrawableWrapperLollipop.java:95)
at android.widget.ImageView.drawableStateChanged(ImageView.java:1129)
at android.support.v7.widget.AppCompatImageButton.drawableStateChanged(AppCompatImageButton.java:149)
at android.view.View.refreshDrawableState(View.java:17099)
at android.support.design.widget.CheckableImageButton.setChecked(CheckableImageButton.java:75)
at android.support.design.widget.TextInputLayout.passwordVisibilityToggleRequested(TextInputLayout.java:1302)
at android.support.design.widget.TextInputLayout$4.onClick(TextInputLayout.java:1076)
at android.view.View.performClick(View.java:5214)
at android.view.View$PerformClick.run(View.java:20978)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

如你所见,问题出在android支持设计库

我在下面找到了这个链接,我们有更多的开发者遇到同样的问题。
Google Issue Tracker: Error Support Design

如果有人知道一些解决方法或解决方案,请告诉我们。

最佳答案

{
    //...
    EditText et;
    //...
    //left, top, right, bottom
    et.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.some_icon, 0);
    et.setOnTouchListener(someTouchListener);
    //...
}

private View.OnTouchListener someTouchListener = new OnTouchListener() {
    final int DRAWABLE_LEFT = 0;
    final int DRAWABLE_TOP = 1;
    final int DRAWABLE_RIGHT = 2;
    final int DRAWABLE_BOTTOM = 3;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (event.getRawX() >= (v.getRight() - ((EditText) v).getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
               //TODO Drawable click here 
                return true;
            }
        }
        return false;
    }
};

关于android - 单击切换按钮时 TextInputLayout (TextInputEditText) NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43980107/

相关文章:

java - 出现奇怪的 filenotfound 异常

java - 在 borderpane 中心加载新的 fxml

java - BlueJ Java MySQL CRUD 应用程序 | BlueJ Java MySQL CRUD 应用程序线程中出现异常 "AWT-EventQueue-0"java.lang.NullPointerException

android:layout_height ="?attr/actionBarSize"不适用于 support:design:23.0.0' 库

android - FloatingActionButton setVisibility() 不工作

android - isDialog() 与 Espresso 中的对话框 fragment 不匹配

Android:当我在单击微调器项目时动态添加新的 ListView 条目时,微调器会丢失它们的值吗?

android - 如何将一项服务绑定(bind)到多个应用程序

nullpointerexception - Elvis 运算符与非空断言 : Diff Between These Statements?

android - 如何以编程方式而不是菜单 xml 将子菜单项添加到 NavigationView