我开发了一个应用程序,我需要通过一个简单的按钮从菜单中启动一个新活动。我做了几个测试并注意到如果我按如下方式设置包含MapFragment的Activity的intent-filter:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
这没有任何崩溃工作正常。 相反,修改意图过滤器的方式是不要在打开应用程序时尽快启动活动,特别是编辑如下:
<action android:name="com.hpdev.maps.MYMAP" />
<category android:name="android.intent.category.DEFAULT" />
活动未启动,应用程序崩溃。我该如何解决这个错误? 我在我的activity.java中包含了代码:
public class MyMap extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
}}
我的堆栈跟踪:
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
答案 0 :(得分:0)
如果你需要做的只是从一个按钮开始一个新的Activity
,只需创建一个Intent
明确指向按钮的onClickListener中的Activity
。以下是在Fragment
按钮中定义的onClickListener:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MapActivity.class);
startActivity(intent);
}
});
您需要确保所有人Activity
都已在AndroidManifest.xml
中注册,但并非所有人都需要使用意图过滤器。您需要Activity
中要启用启动器图标的主/启动器意图过滤器,但其他Activities
不需要意图过滤器,因为您可以在代码中明确引用它们上方。