setSupportActionBar()使我的应用程序失效

时间:2016-04-06 19:21:41

标签: android android-toolbar

出于某种原因,当我把行setSupportActionBar(MyToolBarVariable)...它继续我的Genymotion模拟器..但当它打开应用程序时,它说:

  

Unfourtuanetly“AppName”已停止

但是,如果我删除setSupportActionBar()一切正常......这里的问题是什么..我要发布MainActivity.java文件..如果你需要看到其他人请告诉我如下。谢谢你的帮助!

package com.example.amanuel.zoo4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setContentView(R.layout.activity_main);
    }


}

当我在我的模拟器中按下应用程序时,这是LOGCAT!

04-06 02:09:02.522 652-1011/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.amanuel.zoo4/.MainActivity (has extras)} from uid 10008 on display 0
04-06 02:09:02.721 3360-3360/? I/art: Late-enabling -Xcheck:jni
04-06 02:09:02.762 652-1021/? I/ActivityManager: Start proc 3360:com.example.amanuel.zoo4/u0a63 for activity com.example.amanuel.zoo4/.MainActivity
04-06 02:09:02.815 652-667/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.activityPaused(android.os.IBinder) from ActivityManagerService.java:6439 waiters=1 for 136ms
04-06 02:09:02.978 3360-3360/? W/System: ClassLoader referenced unknown path: /data/app/com.example.amanuel.zoo4-2/lib/x86
04-06 02:09:03.170 3360-3360/? D/AndroidRuntime: Shutting down VM
04-06 02:09:03.178 3360-3360/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.amanuel.zoo4, PID: 3360
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.amanuel.zoo4/com.example.amanuel.zoo4.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
                                                     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:94)
                                                     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:87)
                                                     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:77)
                                                     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                                                     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
                                                     at com.example.amanuel.zoo4.MainActivity.onCreate(MainActivity.java:13)
                                                     at android.app.Activity.performCreate(Activity.java:6237)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-06 02:09:03.193 652-1021/? W/ActivityManager:   Force finishing activity com.example.amanuel.zoo4/.MainActivity
04-06 02:09:03.500 652-1021/? I/WindowManager: Screenshot max retries 4 of Token{75d8954 ActivityRecord{722f0a7 u0 com.example.amanuel.zoo4/.MainActivity t42 f}} appWin=Window{450aeec u0 Starting com.example.amanuel.zoo4} drawState=1
04-06 02:09:03.514 652-652/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12502 waiters=0 for 299ms
04-06 02:09:03.639 652-666/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12502 waiters=1 for 216ms
04-06 02:09:03.640 652-652/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService$UiHandler.handleMessage(android.os.Message) from ActivityManagerService.java:1397 waiters=1 for 111ms
04-06 02:09:03.814 652-1641/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-06 02:09:04.021 652-666/? W/ActivityManager: Activity pause timeout for ActivityRecord{722f0a7 u0 com.example.amanuel.zoo4/.MainActivity t42 f}
04-06 02:09:04.129 652-1641/? W/EGL_emulation: eglSurfaceAttrib not implemented
04-06 02:09:04.130 652-1641/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xd60610a0, error=EGL_SUCCESS
04-06 02:09:04.198 652-667/? I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
04-06 02:09:04.303 652-981/? I/ActivityManager: Killing 1792:com.android.keychain/1000 (adj 15): empty for 3078s
04-06 02:09:04.763 652-668/? E/BluetoothAdapter: Bluetooth binder is null
04-06 02:09:04.825 652-667/? I/Choreographer: Skipped 37 frames!  The application may be doing too much work on its main thread.
04-06 02:09:07.697 3360-3360/? I/Process: Sending signal. PID: 3360 SIG: 9
04-06 02:09:07.732 652-679/? I/ActivityManager: Process com.example.amanuel.zoo4 (pid 3360) has died
04-06 02:09:07.771 652-1641/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xde8f0df0
04-06 02:09:07.844 652-1641/? D/OpenGLRenderer: endAllStagingAnimators on 0xda3b2380 (RippleDrawable) with handle 0xdc57f670
04-06 02:09:07.852 652-652/? W/art: Long monitor contention event with owner method=void android.view.WindowManagerGlobal.removeView(android.view.View, boolean) from WindowManagerGlobal.java:349 waiters=0 for 102ms
04-06 02:09:07.854 652-1164/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@bc8dc8f attribute=null, token = android.os.BinderProxy@4b639cb
04-06 02:09:08.200 652-671/? W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
04-06 02:09:12.630 308-647/? W/AudioFlinger: write blocked for 9842 msecs, 14 delayed writes, thread 0xf1e00000
04-06 02:09:18.081 100-100/? D/Genyd: Received Set Clipboard
04-06 02:09:18.081 100-100/? D/Genymotion: Received Set Clipboard

1 个答案:

答案 0 :(得分:2)

在尝试实例化工具栏之前尝试放置setContentView(R.layout.activity_main);