Android官方教程应用程序崩溃

时间:2015-10-22 12:13:55

标签: android android-layout android-studio

我正在按照官方教程从这里学习Android开发:

http://developer.android.com/training/basics/firstapp/starting-activity.html (开始另一项活动)

以下函数在教程指示与Android Studio生成的代码生成的代码之间有所不同,以创建空白活动。

教程列出的代码:

@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);
}

替换'setContentView()'调用会导致应用程序崩溃。

甚至教程和工作室之间布局的XML文件内容也不同。我想知道我需要做些什么来顺利地学习本教程。

  • 使用早期版本的Android Studio
  • 使用其他SDK版本(Android Studio代码生成依赖于它吗?)

请告知。

错误日志:

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)
....

0 个答案:

没有答案