gpt4 book ai didi

java - 由android.app.fragment$instantiationException导致无法实例化fragment

转载 作者:行者123 更新时间:2023-12-02 05:23:02 25 4
gpt4 key购买 nike

嘿,我有错误,这是我的错误

我正在为 Android 创建示例 map ,但我遇到了这个问题

    10-13 06:31:26.923: D/AndroidRuntime(8754): Shutting down VM
10-13 06:31:26.923: W/dalvikvm(8754): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-13 06:31:26.963: E/AndroidRuntime(8754): FATAL EXCEPTION: main
10-13 06:31:26.963: E/AndroidRuntime(8754): java.lang.RuntimeException: Unable to start activity ComponentInfo{fuad.viewmaps/fuad.viewmaps.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.os.Looper.loop(Looper.java:137)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.reflect.Method.invoke(Method.java:511)
10-13 06:31:26.963: E/AndroidRuntime(8754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-13 06:31:26.963: E/AndroidRuntime(8754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-13 06:31:26.963: E/AndroidRuntime(8754): at dalvik.system.NativeStart.main(Native Method)
10-13 06:31:26.963: E/AndroidRuntime(8754): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-13 06:31:26.963: E/AndroidRuntime(8754): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Activity.setContentView(Activity.java:1881)
10-13 06:31:26.963: E/AndroidRuntime(8754): at fuad.viewmaps.MainActivity.onCreate(MainActivity.java:12)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Activity.performCreate(Activity.java:5104)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-13 06:31:26.963: E/AndroidRuntime(8754): ... 11 more
10-13 06:31:26.963: E/AndroidRuntime(8754): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.map.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Fragment.instantiate(Fragment.java:592)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Fragment.instantiate(Fragment.java:560)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Activity.onCreateView(Activity.java:4709)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-13 06:31:26.963: E/AndroidRuntime(8754): ... 21 more
10-13 06:31:26.963: E/AndroidRuntime(8754): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.map.MapFragment" on path: /data/app/fuad.viewmaps-2.apk
10-13 06:31:26.963: E/AndroidRuntime(8754): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-13 06:31:26.963: E/AndroidRuntime(8754): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-13 06:31:26.963: E/AndroidRuntime(8754): at android.app.Fragment.instantiate(Fragment.java:582)
10-13 06:31:26.963: E/AndroidRuntime(8754): ... 24 more
10-13 06:31:29.103: I/Process(8754): Sending signal. PID: 8754 SIG: 9

这就是我的问题

我的代码

MainActivity.java

package fuad.viewmaps;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView

(R.layout.activity_main); }

}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fuad.viewmaps.MainActivity" >



<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.map.MapFragment"/>

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fuad.viewmaps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCuhxy29DShixyeHjzrB8NxfrQv9TCCZZI"/>


<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>
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<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"/>
<!-- the following two permission are not required to use Google maps Android Api v2,
but are recomended -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

</manifest>

最佳答案

因为你的android:minSdkVersion="8" 。所以,你应该改变这个

<fragment  
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.map.MapFragment"/>

<fragment  
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.map.SupportMapFragment"/>

并且也改变

public class MainActivity extends Activity

public class MainActivity extends FragmentActivity

更新:

此外,您还应该将 Google Play 服务版本添加为 <meta-data>标签 <application>标签在你的 manifest.xml .

   <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

关于java - 由android.app.fragment$instantiationException导致无法实例化fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26334292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com