在appcompat v21上使用OnTouchListener的NullPointerException

时间:2014-10-23 22:16:42

标签: android nullpointerexception android-5.0-lollipop android-appcompat

我正在尝试将我的应用移植到appcompat v21。 我修复了我的gradle文件,我的主题继承自appcompat主题。

public boolean onCreateOptionsMenu(Menu menu) {
        final SharedPreferences touch_prefs = PreferenceManager.getDefaultSharedPreferences(this); //allows preferences to be accessed in the fucntion
       setPassword();


    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.multiscreen, menu);

    final Button decodeButton = (Button) findViewById(R.id.decodeButton); //getting the decode button
    decodeButton.setOnTouchListener(new OnTouchListener() { //setting the touchlistener for the button
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) { //ontouch listener so we can perform actions while the button is held down
            TextView text = (TextView) findViewById(R.id.messageOutput); //so we can set the text of message output
            EditText message_input = (EditText) findViewById(R.id.messageEdit); //gets the message thing
            EditText pass_input = (EditText) findViewById(R.id.passEdit); //gets the password thing
            TextView decode_hint = (TextView) findViewById(R.id.textHint);
            decoder decode = new decoder(); //new instance of classes
            ran verify = new ran(); //new instance of ran class
            boolean legal; //boolean variable that will indicate whether the function should proceed or not

            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    legal = verify.verifyString(message_input); //checks to see if message is aight
                    if (!legal) { //stops function if message contains unsupported characters
                        illegalCharacter(verify.illegal_character);
                    }
                    else if (legal) { //allows functions to proceed if the message is ok
                        s_output = decode.decode_message(message_input, pass_input);
                        decode_hint.setText(R.string.decode_hint_prompt);

                        if (touch_prefs.getBoolean("instaclear_checkbox", true)) { //checking prefs
                            pass_input.setText("");
                        }
                        if (touch_prefs.getBoolean("clear_message_checkbox", true)) { //checking prefs
                            message_input.setText("");
                        }
                        text.setText(s_output); //displays the decoded string
                    }
                    break;
                case MotionEvent.ACTION_UP:

                        if (touch_prefs.getBoolean("passpeek_checkbox", true)) { //checking preferences
                            text.setText("");
                            decode_hint.setText("");
                        }
                    break;
            }
            return true;
        }
    });
    return true;
}

这是返回错误的部分。该错误在此行显示NullPointerException:

decodeButton.setOnTouchListener(new OnTouchListener() {

我的布局/片段的xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rel_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".multiscreen">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/passEdit"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="@string/password_text"
        android:maxLength="500"
        android:layout_toLeftOf="@+id/clearMessage" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageEdit"
        android:hint="@string/message_hint"
        android:layout_below="@+id/passEdit"
        android:layout_marginTop="45dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:maxLength="8000"
        android:layout_toLeftOf="@+id/clearMessage" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/decode_text"
        android:id="@+id/decodeButton"
        android:layout_below="@+id/messageEdit"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:clickable="true"
        android:enabled="true"
        android:focusable="false"
        android:longClickable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textHint"
        android:textIsSelectable="false"
        android:layout_below="@+id/encodeButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/messageOutput"
        android:enabled="true"
        android:textIsSelectable="true"
        android:selectAllOnFocus="true"
        android:layout_below="@+id/textHint"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear_text"
        android:id="@+id/clearMessage"
        android:singleLine="true"
        android:layout_alignTop="@+id/messageEdit"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/messageEdit"
        android:onClick="clearMessage"
        android:enabled="true"
        android:clickable="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/passEdit"
        android:text="@string/clear_text"
        android:id="@+id/clearPassword"
        android:layout_alignRight="@+id/clearMessage"
        android:layout_alignEnd="@+id/clearMessage"
        android:singleLine="true"
        android:layout_above="@+id/messageEdit"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/passEdit"
        android:onClick="clearPassword"
        android:enabled="true"
        android:clickable="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/encode_text"
        android:id="@+id/encodeButton"
        android:layout_below="@+id/messageEdit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:enabled="true"
        android:clickable="true"
        android:onClick="message_encode" />
</RelativeLayout>

这是setcontent部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multiscreen);
}

1 个答案:

答案 0 :(得分:0)

decodeButton为空,因为在您正在使用的任何课程中都找不到它。获取您的布局(rel_layout),然后执行layout.findViewById,因为decodeButtonrel_layout的孩子。

我可能不会在onCreateOptionsMenu中执行此操作,而是在您将布局充气的任何地方。

View layout = getLayoutInflater().inflate(R.layout.rel_layout, null);
Button decodeButton = (Button) layout.findViewById(R.id.decodeButton);
decodeButton.setOnTouchListener(this);