简体   繁体   English

添加工具栏不起作用

[英]Adding a Toolbar doesn't work

I'm trying to integrate a Toolbar to my existing App. 我正在尝试将工具栏集成到我现有的应用程序中。 Android Studio doesn't show any errors but the App just wont work. Android Studio没有显示任何错误,但该应用程序无法正常工作。 It blinks out for half a second or so but then finishes with an info window on my phone which says that my app has been closed. 它会闪烁半秒钟左右,但随后在手机上显示一个信息窗口,提示我的应用已关闭。 What do I do wrong/what is the problem? 我该怎么办/有什么问题?

Here's my whole code: 这是我的整个代码:

MapActivity.Java MapActivity.Java

    package com.example.testingmapingmarker23;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;



import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;



import static com.example.testingmapingmarker23.R.id.map;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);

        final Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar);


    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    }





}

Build.gradle Build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.0'
    defaultConfig {
        applicationId "com.example.testingmapingmarker23"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    //compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.android.gms:play-services-maps:10.2.1'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
    compile "com.android.support:support-core-utils:25.4.0"
}

allprojects {
    repositories {
        jcenter()
        maven{
            url "https://maven.google.com"
        }
    }
}

My device is a OnePlus One with Android 6.0.1 and API 23 on it 我的设备是带有Android 6.0.1和API 23的OnePlus One

Stack Trace with changed Toolbar: 更改工具栏的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.testingmapingmarker23, PID: 2905
                                                                             Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:system, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo}
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testingmapingmarker23/com.example.testingmapingmarker23.MapsActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                 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.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                 at com.example.testingmapingmarker23.MapsActivity.onCreate(MapsActivity.java:35)
                                                                                 at android.app.Activity.performCreate(Activity.java:6251)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                                 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) 

you have an error in xml: 您在xml中有一个错误:

<LinearLayout xmlns:android="http://schemas.android.com/apk/android"
    android:layout_height="1920dp"
    android:layout_widht="1080dp">

there is a typo mistake, change width 出现错字,更改width

    android:layout_width="1080dp">

your stack trace specifies: You must supply a layout_width attribute. 您的堆栈跟踪指定: You must supply a layout_width attribute.

EDIT 编辑

According to your new stack trace: 根据您的新堆栈跟踪:

You need to use a Theme.AppCompat theme (or descendant) with this activity. 您需要在此活动中使用Theme.AppCompat主题(或后代)。

You need to add android:theme="@style/Theme.AppCompat.Light" to your application tag in the AndroidManifest.xml file. 您需要将android:theme="@style/Theme.AppCompat.Light"添加到AndroidManifest.xml文件中的application标签中。

or if you have android:theme="@style/AppTheme" in AndroidManifest.xml file then change your styles.xml with <style name="AppTheme" parent="Theme.AppCompat.Light"> 或者如果您在AndroidManifest.xml文件中具有android:theme="@style/AppTheme" ,然后使用<style name="AppTheme" parent="Theme.AppCompat.Light">更改您的styles.xml

Here are few things you need to keep in mind in order to display your own AppBar / Toolbar 为了显示自己的AppBar / Toolbar您需要记住以下几点

kindly copy this code to your .xml 请将这段代码复制到您的.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="1080dp"
    android:layout_height="1920dp"
    tools:context=".MainActivity">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="1720px"
        tools:context="com.example.testingmapingmarker23.MapsActivity"
        android:layout_gravity="top"/>

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_gravity="bottom"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</LinearLayout>

in your Styles.xml 在您的Styles.xml
Don't provide any action bar 不提供任何动作栏

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

in your MainActivity.java 在您的MainActivity.java

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{
    @Override
    protected void onCreate(){
        ----
        Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar);
        getSupportActionbar.
        setSupportActionBar(customToolbar);
    }

}

That's how you enable your custom AppBar . 这就是您启用自定义AppBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM