0

我開發了一個應用程序,我需要使用一個簡單的按鈕從我的菜單開始一個新活動。我做了一些測試,發現如果我設置活動的意圖過濾器包含MapFragment如下:從另一個活動中使用MapFragment開始活動

<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 
+1

你能提供堆棧跟蹤嗎? http://stackoverflow.com/a/3280126/2668136 – Fllo

回答

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中的Main/Launcher意圖過濾器,但其他Activities不需要意圖過濾器,因爲您可以在上述代碼中進行顯式引用時明確引用它們。

+0

我已經試過這種方法,但它不能解決問題 –

+0

哈利,它看起來像你的stacktrace這一行指出的罪魁禍首:引起:java.lang .ClassNotFoundException:未在路徑中找到類「com.google.android.gms.maps.MapFragment」:DexPathList [[zip文件「/data/app/com.fziviello.altamuragroup-1.apk」],nativeLibraryDirectories = [ /data/app-lib/com.fziviello.altamuragroup-1,/ vendor/lib,/ system/lib]。您是使用Eclipse還是Android Studio?如果使用Android Studio,則需要確保您的依賴包括Google Play服務。 –

+0

賈斯汀我使用Eclipse,無論如何,這不是問題。如果我更改了意圖過濾器的工作原理,所有導入的庫都已啓動並正在運行。 –

相關問題