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!