单击按钮时出现意图错误-为什么?

时间:2019-06-07 12:08:11

标签: java android android-activity oncreate

我遇到了麻烦,无法弄清楚问题出在哪里。 我的代码中没有警报,没有红线或其他任何东西。

HotelMapActivity.java

public class HotelMapActivity extends AppCompatActivity {

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

    }

    public void onClick(View v){
        Intent intent = null;
        switch (v.getId()){
            case R.id.button1:
                intent = new Intent(this, Map1.class);
                break;
        }
        startActivity(intent);
    }
}

这是我的按钮的xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.together.Activities.Map.HotelMapActivity">

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:onClick="onClick"
        tools:context=".Activities.Map.HotelMapActivity"
        android:text="google map"/>


    <android.support.v7.widget.AppCompatButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/button1"
        android:onClick="onClick"
        tools:context=".Activities.Map.HotelMapActivity"
        android:text="place"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/button2"
        android:onClick="onClick"
        tools:context=".Activities.Map.HotelMapActivity"
        android:text="Geo coding"/>
</RelativeLayout>

这是我的Map1类

public class Map1 extends AppCompatActivity implements OnMapReadyCallback {

SupportMapFragment fragment;

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

            fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment1);
    fragment.getMapAsync(this);
}

//비동기적인 방식으로 구글맵을 생성
@Override
public void onMapReady(GoogleMap map) {

    //지도 종류
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    //현재 위치 표현 옵션
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    map.setMyLocationEnabled(true);

    //줌컨트롤 표시 여부 ( 1 ~ 21 )
    map.getUiSettings().setZoomControlsEnabled(true);
}

这是我的Map1活动

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.together.Activities.Map.Map1">


    <fragment
        android:id="@+id/fragment1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="333dp"
        android:layout_height="465dp"
        android:layout_marginStart="36dp"
        android:layout_marginTop="132dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我想知道导致错误发生的原因 因为什么都没有告诉我什么错误

这是我的错误代码

 Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
     Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
        at com.google.maps.api.android.lib6.drd.p.b(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):22)
        at com.google.maps.api.android.lib6.auth.d.a(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):10)
        at com.google.maps.api.android.lib6.impl.d.a(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):41)
        at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):61)
        at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):25)
        at com.google.android.gms.maps.internal.j.a(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):73)
        at hm.onTransact(:com.google.android.gms.dynamite_mapsdynamite@17455084@17.4.55 (100700-248795830):4)
        at android.os.Binder.transact(Binder.java:667)
        at com.google.android.gms.internal.maps.zza.transactAndReadException(Unknown Source:7)
        at com.google.android.gms.maps.internal.zzf.zzc(Unknown Source:8)
        at com.google.android.gms.maps.MapFragment$zzb.zzc(Unknown Source:31)
        at com.google.android.gms.maps.MapFragment$zzb.createDelegate(Unknown Source:2)
        at com.google.android.gms.dynamic.DeferredLifecycleHelper.zza(Unknown Source:44)
        at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(Unknown Source:5)
        at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source:44)
        at android.app.Fragment.onInflate(Fragment.java:1443)
        at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3555)
        at android.app.FragmentController.onCreateView(FragmentController.java:102)
        at android.app.Activity.onCreateView(Activity.java:6323)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:389)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
2019-06-07 20:34:58.118 31313-31313/com.example.blogapp E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.together.Activities.Map.Map1.onCreate(Map1.java:22)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.together">
    <!-- 맵 이용하기 위함 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 다음 지도 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 대략적인 위치 참조 권한 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 정확한 위치 참조 권한 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="i input my key in here" />


        <activity android:name=".Activities.Map.Map1"></activity>

        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />

        <activity android:name=".Activities.MyPetInfo.MyPetRegActivity" />
        <activity android:name=".Activities.MyPetInfo.MyPetListActivity" />
        <activity android:name=".Activities.Petching.MatchesActivity" />
        <activity android:name=".Activities.Petching.SettingsActivity" />
        <activity android:name=".Activities.Map.HotelMapActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activities.GoodbyePet.GoodbyePetActivity" />
        <activity android:name=".Activities.PetHospital.PetHospitalActivity" />
        <activity android:name=".Activities.PetGroup.PetGroupActivity" />
        <activity android:name=".Activities.Petching.PetchingActivity" />
        <activity android:name=".Activities.PetHotel.PetHotelActivity" />
        <activity android:name=".Activities.PetHotel.HotelListDataActivity" />
        <activity android:name=".Activities.PetHotel.HotelDetailActivity" />
        <activity android:name=".Activities.Chat.MessageActivity" />
        <activity android:name=".Activities.Chat.ChatsActivity" />
        <activity android:name=".Activities.OptionActivity" />
        <activity android:name=".Activities.FollowersActivity" />
        <activity android:name=".Activities.EditProfileActivity" />
        <activity android:name=".Activities.CommentsActivity" />
        <activity android:name=".Activities.LoginActivity" />

        <activity android:name=".Activities.RegisterActivity" />
        <activity android:name=".Activities.PostActivity" />
        <activity android:name=".Activities.HomeActivity" />
        <activity
            android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
            android:theme="@style/Base.Theme.AppCompat" />
    </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

在您的AndroidManifest上添加密钥:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_API_KEY"/>

答案 1 :(得分:1)

尝试下面的代码

android:name="com.google.android.gms.maps.MapFragment"

class="com.google.android.gms.maps.SupportMapFragment"

因为您在Java文件中使用了

 fragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment1);
 fragment.getMapAsync(this);

MapFragment不会转换为SupportMapFragment。

希望它对您有用。

答案 2 :(得分:0)

google API最近更新。 Google API客户端也已更新。 google-api-client