我正在开发一个 Android 应用程序,当从列表中选择一个旅游时,它应该启动一个 MapView。但是,每次我尝试通过 Intent 打开此 MapView 时,该应用程序似乎都会崩溃......
这是我的代码:
MainActivity.java:
public class MainActivity extends Activity {
public final static String SELECTED_TOUR = "com.me.tourapp.TOUR";
public static TourListAdapter tourListAdapter;
private static int selectedTour;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
public void startTour(View view) {
Intent intent = new Intent(view.getContext(), TourMapActivity.class);
intent.putExtra(SELECTED_TOUR, selectedTour);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}
}
TourMapActivity.java:
public class TourMapActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour_map);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
//check if tour is passed via intent (test):
Intent intent = getIntent();
int tourIndex = Integer.parseInt(intent.getStringExtra(MainActivity.SELECTED_TOUR));
Toast.makeText(getApplicationContext(), "the tour: "+ tourIndex, Toast.LENGTH_LONG).show();
}
}
activity_tour_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
以及清单中的一些片段:
<permission
android:name="com.me.tourapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.me.tourapp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqs_*****"/>
我还将 android-support-v4.jar 包含到 java 构建路径中,并安装并导入了 google play 服务...
我只是不知道我在哪里犯了错误......
编辑:我添加了 logcat:
E/AndroidRuntime(28248): FATAL EXCEPTION: main
E/AndroidRuntime(28248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hansvn.plaktour/com.hansvn.plaktour.TourMapActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
E/AndroidRuntime(28248): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
E/AndroidRuntime(28248): at android.app.ActivityThread.access$600(ActivityThread.java:140)
E/AndroidRuntime(28248): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
E/AndroidRuntime(28248): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(28248): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(28248): at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(28248): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28248): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(28248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(28248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(28248): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(28248): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(28248): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(28248): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(28248): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(28248): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(28248): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
E/AndroidRuntime(28248): at android.app.Activity.setContentView(Activity.java:1924)
E/AndroidRuntime(28248): at com.hansvn.plaktour.TourMapActivity.onCreate(TourMapActivity.java:16)
E/AndroidRuntime(28248): at android.app.Activity.performCreate(Activity.java:5206)
E/AndroidRuntime(28248): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
E/AndroidRuntime(28248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
E/AndroidRuntime(28248): ... 11 more
E/AndroidRuntime(28248): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(28248): at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
E/AndroidRuntime(28248): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime(28248): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(28248): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E/AndroidRuntime(28248): ... 21 more
E/AndroidRuntime(28248): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
E/AndroidRuntime(28248): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(28248): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(28248): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(28248): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
E/AndroidRuntime(28248): ... 24 more