My MainActivity implements OnClickListener. onCLick works like this: when you click on the title of any list item (Listener is assigned to each header in the binder, this way: view.setOnClickListener (new MainActivity ());) this title (TextView) is replaced with EditText.

Text onClick:

public void onClick(View view) { switch (view.getId()) { case R.id.profileName: String text = (String) (((TextView) view).getText()); LinearLayout layout = (LinearLayout) view.getParent(); layout.removeAllViews(); EditText enterName = new EditText(new MainActivity()); //ERROR HERE enterName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); enterName.setText(text); enterName.setTextSize(TypedValue.COMPLEX_UNIT_SP,45); layout.addView(enterName); 

When creating an EditText program crashes.

 FATAL EXCEPTION: main Process: com.bubbly.freewalkr.tricode, PID: 12673 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.content.ContextWrapper.getResources(ContextWrapper.java:85) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74) at android.view.View.<init>(View.java:3589) at android.view.View.<init>(View.java:3694) at android.widget.TextView.<init>(TextView.java:651) at android.widget.EditText.<init>(EditText.java:67) at android.widget.EditText.<init>(EditText.java:63) at android.widget.EditText.<init>(EditText.java:59) at android.widget.EditText.<init>(EditText.java:55) at com.bubbly.freewalkr.tricode.MainActivity.onClick(MainActivity.java:55) at android.view.View.performClick(View.java:4785) at android.view.View$PerformClick.run(View.java:19858) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5696) 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:1028) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

Apparently, the problem is in the wrong context. I tried different methods, but all tried out gave out the same thing.

How to give context correctly in this case ??

    2 answers 2

    Due to the peculiarities of the work of the Activity and Fragment classes, it is best to assume that they do not have a public constructor. For in your case, for example, you created a class object that is not bound to the system. That's why the fall.

    In your case, the easiest way to get the Context from the argument is: view.getContext()

    • Already tried - the same error. - Freewalkr
    • @Freewalkr, I can not imagine a situation such that it does not work. Add details to the question - Yuriy Spb
    • 2
      @Freewalkr, you assign a click listener completely wrong. You just need to pass this , and not to create activations that are not tied to the system - YuriySPb
    • Try using MainActivity.this.getActivityContext () instead of new MainActivity () - Ivan Vovk
    • @IvanVovk Where to enter it: in the assignment of the listener in the binder or when creating the EditText? In the first case, it writes the MainActivity is not an enclosing class, and in the second, it can't resolve method "getActivityContext ()". What to do?) - Freewalkr

    Found a solution. I wrote a list element in the layout file.
    android:onClick="onClick"
    Now everything works as it should.