I want to make the razm2.xml markup appear on the screen when I click a button, and I have a TextView in this markup, and also after I pressed the button, the text in this TextView changed to mine.

I do this:

 TextView messLenin; Button buttonLenin; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linin); messLenin = (TextView) findViewById(R.id.messLenin); Button buttonLenin = (Button) findViewById(R.id.buttonLenin); buttonLenin.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.buttonLenin: setContentView(R.layout.razm2); messLenin.setText("Мой текст"); break; } } 

But I get a crash from the application after pressing a button:

09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): FATAL EXCEPTION: main 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): java.lang.NullPointerException 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at .mail.xaxa.Lenin.onClick (Lenin.java:54) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at android.view.View.performClick (View .java: 2485) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at android.view.View $ PerformClick.run (View.java:9080) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at android.os.Handler.handleCallback (Handler.javaive87) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at android.os.Handler.dispatchMessage (Handler.java:92) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at android.os.Looper.loop (Looper.java:123) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at android. app.ActivityThread.main (ActivityThread.javaUE683) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at java.lang.reflect.Method.invokeNative (Native Method) 09-27 14: 43: 10.661 : ERROR / AndroidRuntime (1664): at java.lang.reflect.Method.invoke (Method.java:507) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit. java: 839) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java 12.97) 09-27 14: 43: 10.661: ERROR / AndroidRuntime (1664): at dalvik.system.NativeStart.main (Native Method)

And if you remove the replacement text in TextView ( messLenin.setText("Мой текст"); ) after pressing the button, everything is fine, the markup changes ..

I would be grateful for the help!

  • one
    Here is a well-designed question. With a brief listing, a clear description of the desired behavior, an example of attempts to do as it should, and error logs. +1 - Yuriy SPb
  • @YurySPb thanks. - iKey
  • The substitution of the activation markup is a very bad practice that will lead you into big and difficult problems in the future, when there will be more than one widget on the activation. Widgets with different names on different markup are even more evil than the first. Your logic is absolutely not applicable in practice. - pavlofff

1 answer 1

in line

 setContentView(R.layout.razm2); 

The main ContentView and there is no R.id.messLenin or there is a different address. So try adding this.

 setContentView(R.layout.razm2); messLenin = (TextView) findViewById(R.id.messLenin); messLenin.setText("Мой текст"); 
  • I would not advise to encourage solutions to such structurally incorrect approaches. The logic itself is absolutely unviable in real practice - pavlofff