简体   繁体   English

我无法让Google Maps API在我的Android应用程序(V2)上运行

[英]I cannot get Google Maps API to work on my android Application (V2)

In main2.xml there are apparently no errors. main2.xml中,显然没有错误。 Same thing for main.xml however I think the problem lies here somewhere. main.xml也是一样,但是我认为问题出在这里。
Quick Summary of what the app is: It is meant to use Google Maps onClick of a button but I am getting errors so I can just load the main menu on testing. 该应用程序是什么的快速摘要:旨在使用Google Maps onClick的一个按钮,但出现错误,因此只能在测试时加载主菜单。 XML XML格式
I don't have enough rep to post picture so error pic can be found here 我没有足够的代表来发布图片,因此可以在此处找到错误图片

main2.xml main2.xml

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

     <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"/> 

main.xml main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Club Deals"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GPS locations" />
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Closest Deals"
        android:onClick="open_close_deals" />

    <Button
        android:id="@+id/button6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cheapest Deals"
        android:onClick="open_cheap_deals" />


    <Button
        android:id="@+id/button7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Best Value Deals"
        android:onClick="best_value" />

    <Button
        android:id="@+id/button8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Best Events"
        android:onClick="best_events" />

    <Button
        android:id="@+id/button9"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="News" 
        android:onClick="news"/>
</LinearLayout>

AppActivity.java AppActivity.java

In AppActivity I am given "map cannot be resolved or is not a field" & "main2 cannot be resolved or is not a field" 在AppActivity中,我得到“地图无法解析或不是字段”和“ main2无法解析或不是字段”

package com.example.clubnightsdeals;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class AppActivity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    //  addListenerOnButton();
    }

    public void addListenerOnButton() {

        final Context context = this;
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(getApplicationContext(), App2Activity.class);
                startActivity(intent);   

            }
        });
    };
        public void addListenerOnButtonNews() {

        final Context context = this;

        button = (Button) findViewById(R.id.button9);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(getApplicationContext(), App2Activity.class);
                startActivity(intent);   

            }
        });

        };

    /* protected void onCreate1(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main2);
            }

        */
         // Google Map
         GoogleMap googleMap;

        protected void onCreate1(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);

            try {
                // Loading map
                initilizeMap();

            } catch (Exception e) {
                e.printStackTrace();
            }

        } 

        /**
         * function to load map. If map is not created it will create it for you
         * */
        private void initilizeMap() {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }

        @Override
        protected void onResume() {

            super.onResume();
            initilizeMap();
        } 

}      

In App2Activity I am given the error "The import com.example.clubnightsdeals.R cannot be resolved" App2Activity中 ,出现错误“无法解析导入com.example.clubnightsdeals.R”

App2Activity.java App2Activity.java

package com.example.clubnightsdeals;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import com.example.clubnightsdeals.R;

public class App2Activity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    }
 }  

Manifest: 表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.clubnightsdeals"
    android:versionCode="1"
    android:versionName="1.0" >
  <permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

     <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/clubnightsdeals"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <uses-library
        android:name="com.google.android.maps" />
        <activity
            android:label="@string/clubnightsdeals"
            android:name=".App2Activity" >
        </activity>

        <meta-data
     android:name="com.google.android.maps.v2.API_KEY"
     android:value="AIzaSyDetfYqNFORnvtR245fht3RIK25T6DRY" />
          <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>



</manifest>

You are pointing to wrong R.java file. 您指向错误的R.java文件。 Replace your following import line 替换您的以下导入行

import android.R;

with

import com.example.clubnightsdeals.R; 

in your AppActivity.java file. 在您的AppActivity.java文件中。

Your error shows that you have not closed the <activity> tag in your manifest file. 您的错误表明您尚未关闭清单文件中的<activity>标记。
Make sure you have closed your activity tag as <activity android:name=""...../> or <activity android:name=""> </activity> 确保您已关闭<activity android:name=""...../><activity android:name=""> </activity>活动标签。

First remove 首先删除

<intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

in manifest (cannot have 2 start activity in a App) then try to import R library. 在清单中(在App中不能有2个启动活动),然后尝试导入R库。
If it still has this R error, check your xml files - make sure check all xml files. 如果仍然存在此R错误,请检查您的xml文件-确保检查所有xml文件。

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

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