0

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)
....
Malik
  • 87
  • 3
  • 11
  • "Replacing 'setContentView()' call makes the application crash" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Oct 22 '15 at 12:19
  • Thanks. I added beginning part of the log. – Malik Oct 22 '15 at 12:26
  • Could you post your full Java code that results in this crash? It seems getTitle() is being called in your code but I can't see that on what is posted. – Nick Hargreaves Oct 22 '15 at 12:39
  • It seems that getTitle() gets called on a Toolbar that isn't referenced in the code. So it tries to get it's title, but since it doesn't know which Toolbar (since it probably isn't referenced), it crashes. – G.deWit Oct 22 '15 at 12:42
  • Thanks @G.deWit. The problem was because the older Sdk generated code was using AppBatActivity which somehow has tool bar functionality inside. Newer sdk recommends use of AppCompatActivity inside a coordinator layout and uses Toolbar class separately. I got a hang of the newer framework after reading some artivles about the changes. – Malik Nov 05 '15 at 16:36

0 Answers0