Android Studio - "很遗憾' appname'已停止工作"

时间:2015-06-03 05:52:00

标签: java android-activity android-studio toolbar android-appcompat

我正在按照tutorial制作我的第一个基于材料设计的应用。我遇到了几个错误,但已经克服了除应用程序崩溃错误之外的错误。我不知道为什么会这样。我尝试使用API​​ 21和API 22 SDK以及AVD。我也试过真正的设备。我想错误应该是java文件,但不知道在哪个文件,在哪里以及为什么。我这样做是为了我的大学提交。请帮助我。

我的MainActivity.java:

package com.aa.bb.cc;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    private Toolbar mToolbar;

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

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的Activity_Main.xml:

<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">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

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

我的toolbar.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

我的styles.xml:

<resources>

<!-- Base application theme. -->
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Base.Theme.AppCompat.Light.DarkActionBar">


    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>


   </style>
</resources>

LogCat:

    --------- beginning of crash
06-03 11:24:46.739    2065-2065/com.ij.indrajith.firstmaterialdesignapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ij.indrajith.firstmaterialdesignapp, PID: 2065
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ij.indrajith.firstmaterialdesignapp/com.ij.indrajith.firstmaterialdesignapp.MainActivity}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:360)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.ij.indrajith.firstmaterialdesignapp.MainActivity.onCreate(MainActivity.java:16)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

通过查看logcat,我尝试将父主题更改为许多其他主题,但没有用。

更新: 我对主题进行了一些更改,现在错误跳转到:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ij.indrajith.firstmaterialdesignapp/com.ij.indrajith.firstmaterialdesignapp.MainActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to android.support.v7.widget.Toolbar

1 个答案:

答案 0 :(得分:1)

在您使用的布局中:

android.widget.Toolbar

在您正在使用的活动中:

android.support.v7.widget.Toolbar

更改您的布局:

<android.support.v7.widget.Toolbar 
  .... />