Start Activity with MapFragment from another Activ

2019-08-24 19:01发布

问题:

I developed an application and I need to start a new activity from my menu with a simple button. I made several tests and noticed that if I set the intent-filter of Activity that contains the MapFragment as follows:

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

this works fine without any crashes. If instead modify the intent-filter in such a way as not to start the Activity soon as you open the app and in particular the edit as follows:

 <action android:name="com.hpdev.maps.MYMAP" />

 <category android:name="android.intent.category.DEFAULT" />

The activity is not started and the app crashes. How can I fix this bug? I include the code in my activity.java:

public class MyMap extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

}}

My stacktrace:

04-03 20:49:57.637: E/AndroidRuntime(28889): FATAL EXCEPTION: main
04-03 20:49:57.637: E/AndroidRuntime(28889): Process: com.fziviello.altamuragroup, PID: 28889
04-03 20:49:57.637: E/AndroidRuntime(28889): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fziviello.altamuragroup/com.fziviello.altamuragroup.DoveSono}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.os.Looper.loop(Looper.java:136)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.ActivityThread.main(ActivityThread.java:5088)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at java.lang.reflect.Method.invoke(Method.java:515)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at dalvik.system.NativeStart.main(Native Method)
04-03 20:49:57.637: E/AndroidRuntime(28889): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:547)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.inflate(Native Method)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:344)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Activity.setContentView(Activity.java:1952)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at com.fziviello.altamuragroup.DoveSono.onCreate(DoveSono.java:37)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Activity.performCreate(Activity.java:5434)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
04-03 20:49:57.637: E/AndroidRuntime(28889):    ... 12 more
04-03 20:49:57.637: E/AndroidRuntime(28889): 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
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Fragment.instantiate(Fragment.java:597)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Fragment.instantiate(Fragment.java:561)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Activity.onCreateView(Activity.java:4816)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-03 20:49:57.637: E/AndroidRuntime(28889):    ... 25 more
04-03 20:49:57.637: E/AndroidRuntime(28889): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.fziviello.altamuragroup-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.fziviello.altamuragroup-1, /vendor/lib, /system/lib]]
04-03 20:49:57.637: E/AndroidRuntime(28889):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
04-03 20:49:57.637: E/AndroidRuntime(28889):    at android.app.Fragment.instantiate(Fragment.java:583)
04-03 20:49:57.637: E/AndroidRuntime(28889):    ... 28 more

回答1:

If all you need to do is start a new Activity from a button, just create an Intent that explicitly points to that Activity in the button's onClickListener. The below is an onClickListener defined in a Fragment's button:

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), MapActivity.class);
            startActivity(intent);
        }
    });

You need to make sure that all of you Activity's are registered in the AndroidManifest.xml, but they don't all require intent filters. You will need the Main/ Launcher intent filter in the Activity that you want the launcher icon to open, but other Activities don't require intent filters because you can explicitly reference them as I dide in the code above.