在单一布局中尝试放置fragment
容器和Mapfragment
以获得错误。
这里放置xml文件(start.xml)。从我的java类setContentviw(R.layout.start)
。
stacktrace显示
声明地图片段时出现错误
没有得到它。帮助我
<?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:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
日志
09-26 01:59:05.654: E/dalvikvm(1821): Could not find class 'com.google.android.gms.maps.MapFragment', referenced from method com.tukutane.StartActivity.initilizeMap
09-26 01:59:05.654: W/dalvikvm(1821): VFY: unable to resolve check-cast 396 (Lcom/google/android/gms/maps/MapFragment;) in Lcom/tukutane/StartActivity;
09-26 01:59:05.664: D/dalvikvm(1821): VFY: replacing opcode 0x1f at 0x000f
09-26 01:59:05.854: D/AndroidRuntime(1821): Shutting down VM
09-26 01:59:05.854: W/dalvikvm(1821): threadid=1: thread exiting with uncaught exception (group=0x41465700)
09-26 01:59:05.974: E/AndroidRuntime(1821): FATAL EXCEPTION: main
09-26 01:59:05.974: E/AndroidRuntime(1821): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tukutane/com.tukutane.StartActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.os.Looper.loop(Looper.java:137)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-26 01:59:05.974: E/AndroidRuntime(1821): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 01:59:05.974: E/AndroidRuntime(1821): at java.lang.reflect.Method.invoke(Method.java:525)
09-26 01:59:05.974: E/AndroidRuntime(1821): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-26 01:59:05.974: E/AndroidRuntime(1821): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 01:59:05.974: E/AndroidRuntime(1821): at dalvik.system.NativeStart.main(Native Method)
09-26 01:59:05.974: E/AndroidRuntime(1821): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-26 01:59:05.974: E/AndroidRuntime(1821): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Activity.setContentView(Activity.java:1895)
09-26 01:59:05.974: E/AndroidRuntime(1821): at com.tukutane.StartActivity.onCreate(StartActivity.java:34)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Activity.performCreate(Activity.java:5133)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-26 01:59:05.974: E/AndroidRuntime(1821): ... 11 more
09-26 01:59:05.974: E/AndroidRuntime(1821): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Fragment.instantiate(Fragment.java:592)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Fragment.instantiate(Fragment.java:560)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Activity.onCreateView(Activity.java:4738)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-26 01:59:05.974: E/AndroidRuntime(1821): ... 22 more
09-26 01:59:05.974: E/AndroidRuntime(1821): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.tukutane-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.tukutane-1, /system/lib]]
09-26 01:59:05.974: E/AndroidRuntime(1821): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-26 01:59:05.974: E/AndroidRuntime(1821): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-26 01:59:05.974: E/AndroidRuntime(1821): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-26 01:59:05.974: E/AndroidRuntime(1821): at android.app.Fragment.instantiate(Fragment.java:582)
09-26 01:59:05.974: E/AndroidRuntime(1821): ... 25 more
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tukutane"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" >
<!-- Added SplashActivity -->
<activity
android:name="com.tukutane.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Adding MainActivity -->
<activity
android:name="com.tukutane.MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:uiOptions="splitActionBarWhenNarrow" >
</activity>
<!-- Adding StartActivity -->
<activity
android:name="com.tukutane.StartActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:uiOptions="splitActionBarWhenNarrow" >
</activity>
<!-- Adding LoginActivity -->
<activity
android:name="com.tukutane.LoginActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<!-- Adding RegistrationActivity -->
<activity
android:name="com.tukutane.RegistrationActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.parse.integratingfacebook.UserDetailsActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:screenOrientation="portrait" />
<activity
android:name="com.facebook.LoginActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:screenOrientation="portrait" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/maps_key" />
</application>
</manifest>
活性
package com.tukutane;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.tukutane.R.drawable;
public class StartActivity extends Activity {
public static Context appContext;
// Google Map
private GoogleMap googleMap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
appContext = getApplicationContext();
// ActionBar
ActionBar actionbar = getActionBar();
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Setting resource for tab
ActionBar.Tab ProfileTab = actionbar.newTab().setIcon(drawable.profile);
ActionBar.Tab SettingsTab = actionbar.newTab()
.setIcon(drawable.setting);
ActionBar.Tab MessagesTab = actionbar.newTab()
.setIcon(drawable.message);
ActionBar.Tab ContactsTab = actionbar.newTab().setIcon(
drawable.contacts);
ActionBar.Tab DropDownTab = actionbar.newTab().setIcon(drawable.menu);
Fragment ProfileFragment = new ProfileFragment();
Fragment SettingsFragment = new SettingsFragment();
Fragment MessagesFragment = new MessagesFragment();
Fragment ContactsFragment = new ContactsFragment();
Fragment DropdownFragment = new DropdownFragment();
// Setting Tab Listener
ProfileTab.setTabListener(new MyTabsListener(ProfileFragment));
SettingsTab.setTabListener(new MyTabsListener(SettingsFragment));
MessagesTab.setTabListener(new MyTabsListener(MessagesFragment));
ContactsTab.setTabListener(new MyTabsListener(ContactsFragment));
DropDownTab.setTabListener(new MyTabsListener(DropdownFragment));
actionbar.addTab(ProfileTab);
actionbar.addTab(SettingsTab);
actionbar.addTab(MessagesTab);
actionbar.addTab(ContactsTab);
// actionbar.addTab(DropDownTab);
}
private void initilizeMap() {
// TODO Auto-generated method stub
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
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
System.exit(0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem_search:
Toast.makeText(appContext, "suggest places and events",
Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_add:
Toast.makeText(appContext, "Notifications", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.menuitem_share:
TextView inf = new TextView(this);
Toast.makeText(appContext, "Menu", Toast.LENGTH_SHORT).show();
CharSequence[] items = { "Invite friends", "Suggestions", "Points" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
} else if (item == 1) {
} else if (item == 2) {
}
}
});
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow()
.getAttributes();
wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
wmlp.x = 50; // x position
wmlp.y = 75; // y position
dialog.show();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(StartActivity.appContext, "Reselected!",
Toast.LENGTH_LONG).show();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
答案 0 :(得分:1)
这样做
import android.app.Fragment;
import android.app.FragmentTransaction;
替换为
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
答案 1 :(得分:0)
使用此布局
<?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.SupportMapFragment"/>