屏幕上有3个按钮!开始,查看地图,停止
当我点击查看地图时,它应该转到显示地图的新屏幕!但出现了问题,应用程序正在关闭!我相信问题出在xml文件中。请有人纠正。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:id="@+id/widget35"
android:padding="10dp"
android:textSize="35sp"
android:text="Maps"
android:layout_height="wrap_content" />
<Button
android:textSize="30sp"
android:layout_width="wrap_content"
android:id="@+id/buttonStart"
android:text="Start"
android:layout_height="match_parent" />
<Button
android:textSize="30sp"
android:layout_width="wrap_content"
android:id="@+id/buttonMap"
android:text="View Map"
android:layout_height="wrap_content" />
<Button
android:textSize="30sp"
android:layout_width="wrap_content"
android:id="@+id/buttonStop"
android:text="Stop"
android:layout_height="match_parent" />
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:id="@+id/mymap"
android:apiKey="xxxxxxxxxxx" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myzoom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Firstdroid.Gps"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:permission="android.permission.INTERNET"
android:name=".IntentService"
android:enabled="true"
/>
<service android:name="GPSTestApp" />
<activity android:name=".MapViewer">
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
package Firstdroid.Gps;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import Firstdroid.Gps.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
MapView myMap;
private MyLocationOverlay myLocOverlay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* MAP BUTTON */
Button mapButton = (Button) findViewById(R.id.buttonMap);
mapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View m){
Log.d("Firstdroid.Gps", "Loading Map..");
// Loading Google Map View
startService(new Intent(MainActivity.this, MapViewer.class));
}
});
}
}/* End of MainActivity */
除了Map按钮块之外,它还具有启动和停止块,但它们可以正常工作。
package Firstdroid.Gps;
//import android.R;
import Firstdroid.Gps.R;
import android.app.Service;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class MapViewer extends MainActivity {
MapView myMap;
private MyLocationOverlay myLocOverlay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMap();
initMyLocation();
}
private void initMap() {
myMap = (MapView) findViewById(R.id.mymap);
View zoomView = myMap.getZoomControls();
LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
myzoom.addView(zoomView);
myMap.displayZoomControls(true);
}
/**
* Initialises the MyLocationOverlay and adds it to the overlays of the map
*/
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
}
//@Override
protected boolean isRouteDisplayed() {
return false;
}
}
logcat的
06-29 04:34:37.701: WARN/dalvikvm(381): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): FATAL EXCEPTION: main
06-29 04:34:37.731: ERROR/AndroidRuntime(381): java.lang.RuntimeException: Unable to start activity ComponentInfo{Firstdroid.Gps/Firstdroid.Gps.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class com.google.android.maps.MapView
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.os.Looper.loop(Looper.java:123)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at java.lang.reflect.Method.invoke(Method.java:521)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at dalvik.system.NativeStart.main(Native Method)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.google.android.maps.MapView
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.Activity.setContentView(Activity.java:1647)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at Firstdroid.Gps.MainActivity.onCreate(MainActivity.java:23)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): ... 11 more
06-29 04:34:37.731: ERROR/AndroidRuntime(381): Caused by: java.lang.ClassNotFoundException: com.google.android.maps.MapView in loader dalvik.system.PathClassLoader[/data/app/Firstdroid.Gps-2.apk]
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-29 04:34:37.731: ERROR/AndroidRuntime(381): ... 20 more
06-29 04:34:37.751: WARN/ActivityManager(58): Force finishing activity Firstdroid.Gps/.MainActivity
06-29 04:34:38.292: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44fb8580 Firstdroid.Gps/.MainActivity}
06-29 04:34:48.824: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{44fb8580 Firstdroid.Gps/.MainActivity}
答案 0 :(得分:2)
我想你错过了这段代码:
<uses-library android:name="com.google.android.maps" />
在你的android清单中。
答案 1 :(得分:0)
以防万一在模拟器上发生错误:您可能错过了为您的平台安装“Google API”软件包和/或将其用作构建目标和/或未启动 Google API 模拟器。
com.google.android.maps.MapView
的CNFE是一个提示。
Here is a tutorial演示MapView
的用法。检查它是否在目标系统中运行(无ClassNotFoundException
),如果是,则为您自己的应用程序提供模板/模式。
答案 2 :(得分:0)
您需要为地图视图创建单独的布局.xml文件。在res-&gt;布局中创建一个名为map.xml的新xml,并将其过去。
注意:您的MapViewer类应该扩展MapActivity,而不是MainActivity。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="your API key goes here" />
</LinearLayout>
确保将内容视图设置为地图活动中的新文件。
setContentView(R.layout.map);
答案 3 :(得分:0)
我不是百分百确定这是否是问题,但我认为你要两次充气。为什么MapViewer扩展MainActivity而不是Activity,为什么你没有两个XML文件,一个是主活动的布局,另一个是地图查看器的布局?
当您为MapViewer调用onCreate()方法时,首先进行超级调用。
这意味着您通过相同的资源文件有效地调用setContentView()
两次,因为您正在从MainActivity调用SetContentView。我可能是错的,但我认为当您在按下按钮之前创建第一个MainActivity时,您正在创建MapView,然后将其父级设为MainActivity。当您创建MapViewer活动时,您将尝试重新填充视图,而不将其从父活动中删除并重新显示它。
即使我刚才所说的一切都是完全错误的,但是让两个活动共享一个布局文件是非常糟糕的。如果活动都具有完全相同的布局,那么它们应该是一个开始的活动,您应该重新考虑您的方法。