我确实阅读了许多与此错误相关的问题,但没有一个是相关且有帮助的。
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.i / com.example.i.MainActivity}: android.view.InflateException:二进制XML文件行#5:错误 膨胀类android.support.v7.widget.Toolbar
这是我的代码我正在实现工具栏
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
initToolbars();
}
private void initToolbars() {
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.action_settings:
// TODO
break;
// TODO: Other cases
}
return true;
}
});
toolbarBottom.inflateMenu(R.menu.main);
}
acitivity main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_bottom"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:minHeight="?attr/actionBarSize" />
<LinearLayout
android:layout_below="@id/toolbar_top"
android:layout_above="@id/toolbar_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
logcat,如果有人需要它
Process: com.example.i, PID: 15385
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.i/com.example.i.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:340)
at android.app.Activity.setContentView(Activity.java:1973)
at com.example.i.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
... 22 more
Caused by: java.lang.NoSuchFieldError: android.support.v7.appcompat.R$attr.toolbarStyle
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
图书馆的截图
这是我正在使用的导入
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.internal.view.menu.ActionMenuView;
import android.support.v7.widget.Toolbar;
答案 0 :(得分:0)
您需要导入appcompat库
compile 'com.android.support:appcompat-v7:21.0.0'
答案 1 :(得分:0)
您需要将其导入工作区: androidsdk-&GT; extras-&GT; android-&GT;支持 - &GT;设计
然后转到这个&#34;设计&#34;的属性。项目并检查&#34;标记 它作为图书馆&#34;选项。
完成上述要点后,您可以添加此设计项目 到你自己的项目。
之后清理你的项目,然后一切都应该工作。