Issue
There are 3 buttons on the screen! Start, View Map, Stop
When I click View Map, it should go to a new screen that shows the map! But something goes wrong and the app is getting force closed! I believe the problem is with the xml files. Please someone correct it.
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>
MainActivity.java
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 */
In addition to the Map button block, this also has blocks for start and stop, but they work correctly.
MapViewer.java
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}
Solution
You need to create a separate layout .xml file for your map view. Create a new xml called map.xml in res->layout and past this in.
Note: your MapViewer class should extend a MapActivity, not a 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>
Make sure you set the content view to the new file in your map activity.
setContentView(R.layout.map);
Answered By - NSjonas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.