I am following official tutorial to learn Android development from here:
http://developer.android.com/training/basics/firstapp/starting-activity.html (Starting Another Activity)
The following function is different between what the tutorial instructs to do vs code generated by Android Studio's generated one to create a Blank Activity.
Tutorial Listed Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Android studio Generated:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
Replacing 'setContentView()' call makes the application crash.
Even content in XML files for layouts is different between tutorial and studio. I am wondering what I need to do to follow the tutorial smoothly.
- Use an earlier version of Android Studio
- Use a different SDK version (does Android Studio code generation depend on it?)
Please advise.
Error Log:
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: FATAL EXCEPTION: main
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: Process: com.greenbergc.mysecondapp, PID: 22824
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.greenbergc.mysecondapp/com.greenbergc.mysecondapp.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:177)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5942)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
10-22 16:23:32.528 22824-22824/com.greenbergc.mysecondapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
....