我想在没有支持库的情况下为Android App添加工具栏。我试了一个例子 https://gist.github.com/gabrielemariotti/ae63392e1c70bc33af8b 但是,当我运行此示例时,它显示应用程序停止工作
修改 Log cat如下
03-09 11:13:35.969 16807 16807 D dalvikvm Late-enabling CheckJNI
03-09 11:13:36.159 16807 16807 D ActivityThread handleBindApplication:com.mycompany.myapp2
03-09 11:13:36.479 16807 16807 E dalvikvm Could not find class 'android.widget.Toolbar', referenced from method com.mycompany.myapp2.MainActivity.onCreate
03-09 11:13:36.479 16807 16807 W dalvikvm VFY: unable to resolve check-cast 24 (Landroid/widget/Toolbar;) in Lcom/mycompany/myapp2/MainActivity;
03-09 11:13:36.499 16807 16807 D dalvikvm VFY: replacing opcode 0x1f at 0x001a
03-09 11:13:36.499 16807 16807 I dalvikvm Failed resolving Lcom/mycompany/myapp2/MainActivity$100000000; interface 23 'Landroid/widget/Toolbar$OnMenuItemClickListener;'
03-09 11:13:36.499 16807 16807 W dalvikvm Link of class 'Lcom/mycompany/myapp2/MainActivity$100000000;' failed
03-09 11:13:36.499 16807 16807 D dalvikvm DexOpt: unable to opt direct call 0x003f at 0x30 in Lcom/mycompany/myapp2/MainActivity;.onCreate
03-09 11:13:36.539 16807 16807 W ApplicationPackageManager getCSCPackageItemText()
03-09 11:13:36.549 16807 16807 D DisplayManager DisplayManager()
03-09 11:13:36.759 16807 16807 D AndroidRuntime Shutting down VM
03-09 11:13:36.759 16807 16807 W dalvikvm threadid=1: thread exiting with uncaught exception (group=0x417bebc0)
03-09 11:13:36.789 16807 16807 E AndroidRuntime FATAL EXCEPTION: main
03-09 11:13:36.789 16807 16807 E AndroidRuntime Process: com.mycompany.myapp2, PID: 16807
03-09 11:13:36.789 16807 16807 E AndroidRuntime java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp2/com.mycompany.myapp2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class Toolbar
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.ActivityThread.access$800(ActivityThread.java:166)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:102)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.os.Looper.loop(Looper.java:136)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5584)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:515)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
03-09 11:13:36.789 16807 16807 E AndroidRuntime Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class Toolbar
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.Activity.setContentView(Activity.java:2018)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at com.mycompany.myapp2.MainActivity.onCreate(MainActivity.java:13)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.Activity.performCreate(Activity.java:5442)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
03-09 11:13:36.789 16807 16807 E AndroidRuntime ... 11 more
03-09 11:13:36.789 16807 16807 E AndroidRuntime Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Toolbar" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp2-25.apk"],nativeLibraryDirectories=[/data/app-lib/com.mycompany.myapp2-25, /vendor/lib, /system/lib]]
03-09 11:13:36.789 16807 16807 E AndroidRuntime at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.createView(LayoutInflater.java:566)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.onCreateView(LayoutInflater.java:659)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.onCreateView(LayoutInflater.java:676)
03-09 11:13:36.789 16807 16807 E AndroidRuntime at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:701)
03-09 11:13:36.789 16807 16807 E AndroidRuntime ... 21 more
答案 0 :(得分:1)
android.widget.Toolbar仅在API 21(Android 5.0 Lollipop)中添加。因此,您无法在该API级别之前使用它 - 这正是android.support.v7.widget.Toolbar提供的AppCompat所提供的 - Toolbar
可以回溯到API 7。
答案 1 :(得分:0)
只需创建一个自定义工具栏,就像在水平线性布局中一样。并将您的自定义布局包含在您需要的其他布局文件中。