0

Im setting up a kisok App and keep getting a java.lang.NullPointerException in my WebviewActivity.

Unable to start activity ComponentInfo{eu.xx.xyc/eu.xx.xyz.ActivityWebView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference

Checking xml for "view" instead of "View" declarations. Checking if the Object has values, also if the object is instanciated. Have looked in Stackoverflow but the given solution do not work for me.

        <EditText
            android:layout_width="289dp"
            android:layout_height="wrap_content"
            android:inputType="textUri"
            android:text="@string/not_set"
            android:id="@+id/remoteContentURLTextField"
            android:layout_below="@+id/remoteContentURL"
            android:layout_alignParentStart="true" />

private EditText mRemoteContentURLEditText;

protected void onCreate(Bundle savedInstanceState) {

 setContentView(R.layout.activity_web_view);

connectUiElements();
setUpUiListeners();
}
 private void connectUiElements() {
        mRemoteContentURLEditText = (EditText) findViewById(R.id.remoteContentURLTextField);
}
mRemoteContentURLEditText.addTextChangedListener(
                new TextWatcher() {

                    @Override
                    public void afterTextChanged(Editable s) {
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start,
                                                  int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence newValue, int start,
                                              int before, int count) {
                        updateRemoteContentURL(newValue.toString());
                    }
                }
        );

        mRemoteContentURLEditText.setOnEditorActionListener(

                new EditText.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        return false; // pass on to other listeners.
                    }
                }
        );

Im expecting it to build normally and not to be null.

Thanks for help.

NobodyIsPerfect
  • 182
  • 1
  • 12
  • 1
    Are you calling `setContentView()` before `connectUiElements();`? If so, are you sure that `` with ID `remoteContentURLTextField` is in the layout you're passing to `setContentView()`? – Mike M. Aug 21 '19 at 12:55
  • yep i do actually. I Updated my code. – NobodyIsPerfect Aug 21 '19 at 13:14
  • Then it would seem that the `` with ID `remoteContentURLTextField` is not in the currently-loaded `activity_web_view` layout. Is that the layout it's in? Do you possibly have more than one version of that layout; e.g., one each for portrait and landscape, or for different screen sizes? – Mike M. Aug 21 '19 at 13:19
  • wow... you are right. This project actually do have 2 activity_web_view.xml 1 general and 1 for 720p... thanks :D... i feel dumb, that I did not see it for like 4 hours... >. – NobodyIsPerfect Aug 21 '19 at 13:40
  • Yep, if you unconditionally use that `EditText`, you need to make sure it's in all versions: https://stackoverflow.com/a/8018120. Cool. No problem. Glad you got it figured out. Cheers! – Mike M. Aug 21 '19 at 13:42

0 Answers0