Android google map - android

I am try to add Google MAP Api v2 in my application.but I am getting exception. Exception is
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
Here is my xml class
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
this is my main class
public class MainActivity extends Activity {
private GoogleMap _googleMap=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
//loading map
initilizeMap();
_googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
_googleMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if(_googleMap==null)
{
_googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if (_googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
initilizeMap();
}
}
this is my logcat
01-31 14:20:55.603: E/AndroidRuntime(311): FATAL EXCEPTION: main
01-31 14:20:55.603: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.os.Looper.loop(Looper.java:123)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 14:20:55.603: E/AndroidRuntime(311): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:20:55.603: E/AndroidRuntime(311): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 14:20:55.603: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 14:20:55.603: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 14:20:55.603: E/AndroidRuntime(311): at dalvik.system.NativeStart.main(Native Method)
01-31 14:20:55.603: E/AndroidRuntime(311): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-31 14:20:55.603: E/AndroidRuntime(311): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.Activity.setContentView(Activity.java:1647)
01-31 14:20:55.603: E/AndroidRuntime(311): at com.example.demo.MainActivity.onCreate(MainActivity.java:22)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 14:20:55.603: E/AndroidRuntime(311): ... 11 more
01-31 14:20:55.603: E/AndroidRuntime(311): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.demo-2.apk]
01-31 14:20:55.603: E/AndroidRuntime(311): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-31 14:20:55.603: E/AndroidRuntime(311): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-31 14:20:55.603: E/AndroidRuntime(311): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
01-31 14:20:55.603: E/AndroidRuntime(311): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-31 14:20:55.603: E/AndroidRuntime(311): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

You should use SupportMapFragment instead of MapFragment.
Change
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
to
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Also change
_googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
to
_googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
And also If you're using fragments on older devices (pre Honeycomb) you should always extend your Activity from FragmentActivity.
and also check
Add the Google Play services version to your app's manifest
Edit your application's AndroidManifest.xml file, and add the
following declaration within the element. This embeds the version of
Google Play services that the app was compiled with.
You just need to add <meta-data> under <application> tag into your AndroidManifest.xml
....<application>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
This is because latest google play services requires a version name, which is to be mentioned using <meta-data .. /> inside AndroidManifest.xml

Extend your MainActivity from FragmentActivity.
Refer this :
Inflating Google Maps v2 fragment causes ClassNotFoundException

If you are using Fragments your MainActivity must extends FragmentActivity:
public class MainActivity extends FragmentActivity {
//Stuff
}
If you are using the Support-Library so you can use your Fragments in lower Android API Levels use This:
public class MainActivity extends android.support.v4.app.FragmentActivity{
//Stuff
}
And use the SupportFragmentMap in your XML:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

In your xml try to change from
android:name="com.google.android.gms.maps.MapFragment"
to
android:name="com.google.android.gms.maps.SupportMapFragment"
Check if this work.

Related

Android Manifest change force closes app

I added a class to my working app. I want the new class to be the first page to load, its called dashboard.class. I added it to the manifest and now the app force closes when you try to load it. I also went and removed the entry in the manifest and the app worked again. Please tell me what I am doing wrong
<activity
android:name="com.magicbuddy.gamble.Dashboard"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.magicbuddy.gamble.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.MagicBuddy.Gamble.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The dashboard is the new class I added. When I remove it and reset MainActivity to LAUNCHER and MAIN, the app will work just fine again.
I tried the posted solutions of removing the intent-filter and it is still crashing. Here is the logcat **Also thank you guys for your replys
01-31 13:56:38.709: D/AndroidRuntime(1995): Shutting down VM
01-31 13:56:38.709: W/dalvikvm(1995): threadid=1: thread exiting with uncaught exception (group=0xb2eda288)
01-31 13:56:38.709: E/AndroidRuntime(1995): FATAL EXCEPTION: main
01-31 13:56:38.709: E/AndroidRuntime(1995): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.magicbuddy.gamble/com.magicbuddy.gamble.Dashboard}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.os.Looper.loop(Looper.java:137)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-31 13:56:38.709: E/AndroidRuntime(1995): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 13:56:38.709: E/AndroidRuntime(1995): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 13:56:38.709: E/AndroidRuntime(1995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-31 13:56:38.709: E/AndroidRuntime(1995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-31 13:56:38.709: E/AndroidRuntime(1995): at dalvik.system.NativeStart.main(Native Method)
01-31 13:56:38.709: E/AndroidRuntime(1995): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
01-31 13:56:38.709: E/AndroidRuntime(1995): at com.magicbuddy.gamble.Dashboard.onCreate(Dashboard.java:25)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.Activity.performCreate(Activity.java:5008)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-31 13:56:38.709: E/AndroidRuntime(1995): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
I added a class that has only 4 buttons on it and each button has an image. That is what I want to load first but this is the logcat error when I try to make that happen
According to the logcat the Exception is:
java.lang.ClassCastException: android.widget.ImageButton cannot be
cast to android.widget.Button
This is happening in the onCreate() method of your Dashboard Activity. You are casting an ImageButton to a Button. The problem is not in the manifest but in the class itself.
Try removing the intent-filter from your MainActivity. You really only need to define intent filters when trying to control what types of intents access into your activity, but if that is only going to be called upon by your Dashboard activity as I think it is, then you don't need to include the intent filter as it is part of the same package

Google Maps Android API v2 and Eclipse

I am new in Google Map and so many things aren't clear enough to me for that reason.So i expect some expert's direction on this regard.I read This Guide and did everything what it says.I use google map api v2 and get a map key.Then i make a sample project following that link and select target version android 2.2(while running the project).But a list of error occurs after running then example in eclipse android emulator.My logcat view shows:
01-31 00:51:59.226: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-31 00:51:59.236: E/AndroidRuntime(329): FATAL EXCEPTION: main
01-31 00:51:59.236: E/AndroidRuntime(329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.googleMapApp/com.googleMapApp.LocationProfileManagerActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.os.Looper.loop(Looper.java:123)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 00:51:59.236: E/AndroidRuntime(329): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 00:51:59.236: E/AndroidRuntime(329): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 00:51:59.236: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 00:51:59.236: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 00:51:59.236: E/AndroidRuntime(329): at dalvik.system.NativeStart.main(Native Method)
01-31 00:51:59.236: E/AndroidRuntime(329): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-31 00:51:59.236: E/AndroidRuntime(329): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.Activity.setContentView(Activity.java:1647)
01-31 00:51:59.236: E/AndroidRuntime(329): at com.googleMapApp.LocationProfileManagerActivity.onCreate(LocationProfileManagerActivity.java:14)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 00:51:59.236: E/AndroidRuntime(329): ... 11 more
01-31 00:51:59.236: E/AndroidRuntime(329): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.googleMapApp-1.apk]
01-31 00:51:59.236: E/AndroidRuntime(329): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-31 00:51:59.236: E/AndroidRuntime(329): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-31 00:51:59.236: E/AndroidRuntime(329): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
01-31 00:51:59.236: E/AndroidRuntime(329): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-31 00:51:59.236: E/AndroidRuntime(329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
My Manifest.xml is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googleMapApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<permission
android:name="com.googleMapApp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.googleMapApp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Require OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".LocationProfileManagerActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myMapKey"/>
</application>
</manifest>
My main.xml is:
<?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"
class="com.google.android.gms.maps.MapFragment"/>
My activity.java is:
package com.googleMapApp;
import android.app.Activity;
import android.os.Bundle;
public class LocationProfileManagerActivity extends Activity {
/** Called when the activity is first created. */
//private GoogleMap map;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
Sorry for this lengthy description,but i want to show you exactly what i did.I think as a newcomer i did a lot of mistake on this way.so i expect someone will guide me that.
You are using features only available in later APIs, namely Fragment. If you want to compile to support 2.2, you need to include the Support Library in your project. You will also have to make the following changes to use support fragments instead of regular fragments --
In main.xml, change
class="com.google.android.gms.maps.MapFragment"/>
to
class="com.google.android.gms.maps.SupportMapFragment"/>
and in your activity.java, change
public class LocationProfileManagerActivity extends Activity {
to
public class LocationProfileManagerActivity extends FragmentActivity {
you need to make two changes
Since as you are sdk version is below 12, need to make following change
1)
main.xml
Replace
class="com.google.android.gms.maps.MapFragment"/>
with
class="com.google.android.gms.maps.SupportMapFragment"/>
2) activity.java
Need to extend FragmentActivity instead of Activity
If you want to test in emulator , then download the google_play_services_apk and install it in emulator.
Maps API v2 cannot be run in an emulator because of its dependency on Google Play Services, which is bundled in the Google Play Store .apk. It can be hacked together though, you can read about it here on the first Google result for "Google maps v2 emulator". Also you can still use V1 for a limited time, you can get started here.

following exception in android simulator 2.0

i got following strange exception on only andorid simulator 2.0
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.markupartist.android.example.pulltorefresh/com.markupartist.android.example.pulltorefresh.PullToRefreshActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.markupartist.android.widget.PullToRefreshListView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.markupartist.android.widget.PullToRefreshListView
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
at android.app.Activity.setContentView(Activity.java:1647)
at com.markupartist.android.example.pulltorefresh.PullToRefreshActivity.onCreate(PullToRefresh Activity.java:21)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at com.markupartist.android.widget.PullToRefreshListView. (PullToRefreshListView.java:54)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 21 more
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030002
at android.content.res.Resources.getValue(Resources.java:892)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
at android.content.res.Resources.getLayout(Resources.java:731)
at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.markupartist.android.widget.PullToRefreshListView.init(PullToRefreshListView.java:80)
... 25 more
when i tested on andorid 1.6 , 2.01,2.1,2.2,2.3 ,4.0 it works completely,
is this a simulator bug of 2.0
please give me some information about this
Thanks

RuntimeException: Error inflating class fragment

I am getting a runtimeException with the following code, does anyone know why?
public class FragsApplicationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
SuggestionFrags class
public class SuggestionFrags extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
main.xml
<?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="vertical" >
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.samplefragsapplication.SuggestionFrags" />
</LinearLayout>
I am getting this exception:
11-23 18:09:23.899: E/AndroidRuntime(2436): FATAL EXCEPTION: main
11-23 18:09:23.899: E/AndroidRuntime(2436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samplefragsapplication/com.samplefragsapplication.FragsApplicationActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.os.Looper.loop(Looper.java:123)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.reflect.Method.invoke(Method.java:521)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 18:09:23.899: E/AndroidRuntime(2436): at dalvik.system.NativeStart.main(Native Method)
11-23 18:09:23.899: E/AndroidRuntime(2436): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.Activity.setContentView(Activity.java:1647)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.samplefragsapplication.FragsApplicationActivity.onCreate(FragsApplicationActivity.java:12)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-23 18:09:23.899: E/AndroidRuntime(2436): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.samplefragsapplication-2.apk]
11-23 18:09:23.899: E/AndroidRuntime(2436): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
If you are going to use Fragments you need to extend FragmentActivity not Activity.
To fix this issue you have to extends from FragmentActivity instead of Activity in the FragsApplicationActivity.
The proper form will be
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.samplefragsapplication.SuggestionFrags" />
UPD: judging by the logcat output, you don't have fragments available. If you're running a pre-Honeycomb android (but >= 1.6), you can use the compatibility library. Fragments were introduced in 3.0.
The exception android.view.InflateException: Binary XML file line: #... Error inflating class fragment might happen if you manipulate with getActivity() inside your fragment before onActivityCreated() get called. In such case you receive a wrong activity reference and can't rely on that.
For instance the next pattern is wrong:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View view = inflater.inflate(R.layout..., container, false);
Button button = getActivity().findViewById(R.id...);
button.setOnClickListener(...); - another problem: button is null
return view;
}
I just had the same issue, and what I found was that in my main.xml I was incorrectly referencing the fragment class.
Instead of:
<fragment android:id="#+id/frag_capt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.company.test.applicationname.fragmentname" />
try:
<fragment android:id="#+id/frag_capt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.company.test.DetailFrag" />
This fixed the issue for me.
Good luck.
Try this
<fragment
android:id="#+id/fragment1"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
Make sure android:name="className" is the first parameter in the list
ex
<fragment
android:name="com.name.first"
android:id="#+id/"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
I had the same issue, to summarise:
Extend from FragmentActivity
Follow this structure in your layout
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.MapFragment"
/>

inflating fragments with compatibility package android

I am trying to inflate a layout containing a Fragment using the backwards compatibility package. I took the jar file and placed it in the libs folder of my project. I extended Fragment and then tried to inflate it by setting the contentView of the Activity to
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment
class="com.test.fragments.AdFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/></LinearLayout>
But when I set the content view it fails with a ClassNotFoundException for the fragment tag.
Here is the logcat output.
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: \
android.view.InflateException: Binary XML file line #51: \
Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1777)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1793)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3848)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #51: \
Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:211)
at android.app.Activity.setContentView(Activity.java:1657)
at com.test.base.activities.TabbedStoreActivity.onCreate(TabbedStoreActivity.java:46)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1731)
... 11 more
Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader \
dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar: \
/data/app/com.test.test.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.view.LayoutInflater.createView(LayoutInflater.java:471)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
... 20 more
Make sure your activity is inheriting from FragmentActivity, otherwise <fragment> does not work. Here is a sample project demonstrating this.

Categories

Resources