尝试添加工具栏给AndroidRuntime:FATAL EXCEPTION:main

时间:2016-01-10 19:04:09

标签: java android runtime-error material-design android-appcompat

我正在尝试为我的应用添加工具栏。我一直在关注一个教程,但我仍然无法让它工作。我会发布我认为与错误有关的代码,但如果你还需要其他任何东西,请告诉我。

MainActivity.java:

package riveras.kasparsweblser;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

WebView webview;

private WebView mWebView;
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar= (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    String url = "http://www.google.com";
    webview = (WebView) findViewById(R.id.wv);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl(url);
    webview.setWebChromeClient(new WebChromeClient());
    webview.setWebViewClient(new WebViewClient());
}

@Override //Add menu items later
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

@Override //Go to previous page when Back is clicked
public boolean onKeyDown(int keyCode, KeyEvent event) {
    mWebView = (WebView) findViewById(R.id.wv);
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
 }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
            android:layout_height="match_parent" android:fitsSystemWindows="true"
            tools:context=".MainActivity">
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity" >

            <include
                android:id="@+id/app_bar"
                layout="@layout/app_bar" />

            <WebView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/wv"
                android:layout_alignParentBottom="true"
                android:elevation="2dp"
                android:layout_gravity="center" />

        </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

app_bar.xml(我要添加的工具栏):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.Toolbar>

我收到了这个我无法理解的错误:

01-10 19:27:10.842 17194-17194/riveras.kasparsweblser E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: riveras.kasparsweblser, PID: 17194
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{riveras.kasparsweblser/riveras.kasparsweblser.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.Toolbar
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5291)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.Toolbar
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
                                                                        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                        at riveras.kasparsweblser.MainActivity.onCreate(MainActivity.java:22)
                                                                        at android.app.Activity.performCreate(Activity.java:5990)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418) 
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5291) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
                                                                     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.Toolbar" on path: DexPathList[[zip file "/data/app/riveras.kasparsweblser-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                                                                        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:571)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                                        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892) 
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                        at riveras.kasparsweblser.MainActivity.onCreate(MainActivity.java:22) 
                                                                        at android.app.Activity.performCreate(Activity.java:5990) 
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309) 
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418) 
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5291) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
                                                                        Suppressed: java.lang.ClassNotFoundException: android.support.v7.Toolbar
                                                                        at java.lang.Class.classForName(Native Method)
                                                                        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                ... 25 more
                                                                     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

如果有人确实看到了什么错,我会非常感谢知道,任何有助于解决我的问题的事情都值得赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您使用的是错误的包

在布局aap_bar.xml文件中使用android.support.v7.widget.Toolbar代替android.support.v7.Toolbar