android.view.InflateException:在PopMainActivity中填充类片段时出错

时间:2016-02-02 14:51:13

标签: android android-fragments

我在点击mapButton id时使用弹出对话框。在弹出对话框中必须显示地图。点击mapbutton后我收到错误inflating class fragment exception。我已发布相关代码。

我已经针对这些问题尝试了很多Stackoverflow帖子。没有解决我的问题。

堆栈跟踪:

02-02 09:27:52.241: E/AndroidRuntime(13380): FATAL EXCEPTION: main
02-02 09:27:52.241: E/AndroidRuntime(13380): Process: com.android.steveactiivty, PID: 13380
02-02 09:27:52.241: E/AndroidRuntime(13380): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at com.android.steveactiivty.PopMainActivity$1.onClick(PopMainActivity.java:52)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.View.performClick(View.java:4756)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.View$PerformClick.run(View.java:19749)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.os.Handler.handleCallback(Handler.java:739)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.os.Looper.loop(Looper.java:135)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.app.ActivityThread.main(ActivityThread.java:5221)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at java.lang.reflect.Method.invoke(Native Method)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at java.lang.reflect.Method.invoke(Method.java:372)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-02 09:27:52.241: E/AndroidRuntime(13380): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.fragment" on path: DexPathList[[zip file "/data/app/com.android.steveactiivty-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
02-02 09:27:52.241: E/AndroidRuntime(13380):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.createView(LayoutInflater.java:571)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:665)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:65)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
02-02 09:27:52.241: E/AndroidRuntime(13380):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
02-02 09:27:52.241: E/AndroidRuntime(13380):    ... 15 more
02-02 09:27:52.241: E/AndroidRuntime(13380):    Suppressed: java.lang.ClassNotFoundException: android.view.fragment
02-02 09:27:52.241: E/AndroidRuntime(13380):        at java.lang.Class.classForName(Native Method)
02-02 09:27:52.241: E/AndroidRuntime(13380):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
02-02 09:27:52.241: E/AndroidRuntime(13380):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
02-02 09:27:52.241: E/AndroidRuntime(13380):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
02-02 09:27:52.241: E/AndroidRuntime(13380):        ... 21 more
02-02 09:27:52.241: E/AndroidRuntime(13380):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

PopMainActivity.java:

 public class PopMainActivity extends FragmentActivity {
 Button mapButton,dismiss;  
    View popupView;

    // Google Map
        private GoogleMap googleMap;

        EditText editLong,editLat;

    @SuppressWarnings("deprecation")  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapButton = (Button) findViewById(R.id.mapButton);


        editLat = (EditText)findViewById(R.id.editLat);
        editLong = (EditText)findViewById(R.id.editLong);

        mapButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);

                popupView = layoutInflater.inflate(R.layout.popup_main, null);

                final PopupWindow popupWindow = new PopupWindow(popupView,
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                getPopUpView();

                SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.popMapFrag);

                // Getting a reference to the map
                googleMap = supportMapFragment.getMap();

                // popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);


                // Setting a click event handler for the map
                googleMap.setOnMapClickListener(new OnMapClickListener() {

                    @Override
                    public void onMapClick(LatLng latLng) {  

                        // Creating a marker
                        MarkerOptions markerOptions = new MarkerOptions();   

                        // Setting the position for the marker
                        markerOptions.position(latLng);

                        // Setting the title for the marker.
                        // This will be displayed on taping the marker
                        markerOptions.title(latLng.latitude + " : " + latLng.longitude);

                        editLat.setText(Double.toString((double) latLng.latitude));
                        editLong.setText(Double.toString((double) latLng.longitude));

                        // Clears the previously touched position
                        googleMap.clear();

                        // Animating to the touched position
                        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                        // Placing a marker on the touched position
                        googleMap.addMarker(markerOptions);
                    }
                });
                    popupWindow.showAsDropDown(mapButton, 50, -30);

            }

            private void getPopUpView() {

            //  dismiss=(Button)popupView.findViewById(R.id.dismiss);


            }
        });
 }
 }

popup_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical" >

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

       </LinearLayout>  

2 个答案:

答案 0 :(得分:1)

替换:

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);

使用:

LayoutInflater layoutInflater = getLayoutInflater();

除非您确切知道为什么使用LayoutInflate.from(),否则永远不要使用LayoutInflater.from()。特别是,当您尝试对其中包含LayoutInflater.from()元素的布局进行充气时,请不要使用<fragment>。另外,如果您有自定义主题,请不要使用LayoutInflater.from()。如果您正在处理某项活动,请始终从LayoutInflater获取getLayoutInflater()

在这种情况下,不清楚为什么布局中有一个<fragment>元素,因为布局没有任何其他价值。而且我不确定你是否可以使用PopupWindow中的片段。如果您必须摆脱所有这些并使用Maps V2的MapView版本,我不会感到惊讶。

答案 1 :(得分:0)

尝试支持片段 由于您使用的是FragmentActivity和SupportMapFragment,因此<android.support.v4.app.Fragment />代替<fragment />