无法启动Componentinfo

时间:2014-06-30 17:01:41

标签: android

好吧,我正在尝试将地图放在一个操作栏项上,并有这个日志

06-30 12:51:28.355: E/AndroidRuntime(1417): FATAL EXCEPTION: main
06-30 12:51:28.355: E/AndroidRuntime(1417): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.larissaevent/org.example.larissaevent.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.os.Looper.loop(Looper.java:137)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at java.lang.reflect.Method.invoke(Method.java:525)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at dalvik.system.NativeStart.main(Native Method)
06-30 12:51:28.355: E/AndroidRuntime(1417): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.Activity.setContentView(Activity.java:1895)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at org.example.larissaevent.MapActivity.onCreate(MapActivity.java:23)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.Activity.performCreate(Activity.java:5133)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-30 12:51:28.355: E/AndroidRuntime(1417):     ... 11 more
06-30 12:51:28.355: E/AndroidRuntime(1417): Caused by: java.lang.ClassCastException: org.example.larissaevent.MapActivity cannot be cast to android.app.Fragment
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.Fragment.instantiate(Fragment.java:585)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.Fragment.instantiate(Fragment.java:560)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.app.Activity.onCreateView(Activity.java:4738)
06-30 12:51:28.355: E/AndroidRuntime(1417):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-30 12:51:28.355: E/AndroidRuntime(1417):     ... 21 more

这是MainActivity for action BAr的代码

public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent= null;
        switch (item.getItemId()) {
        case R.id.action_activity1:

            intent =new Intent(MainActivity.this,MapActivity.class);
            startActivity(intent);

            break;

        case R.id.action_activity2:
            String facebook ="https://www.facebook.com/";
            intent =new Intent(Intent.ACTION_VIEW, Uri.parse(facebook));
            startActivity(intent);
            break;

        case R.id.action_activity4:
            String facebook1 ="https://www.facebook.com/";
            intent =new Intent(Intent.ACTION_VIEW, Uri.parse(facebook1));
            startActivity(intent);
            break;




        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

这是地图活动

package org.example.larissaevent;



import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MapActivity extends FragmentActivity {



    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }





    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            if (googleMap != null) {
                // The Map is verified. It is now safe to manipulate the map.

            }

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }



}

这是我的清单

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

    <uses-sdk android:minSdkVersion="14" 
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <permission
        android:name="org.example.larissaevent.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="org.example.larissaevent.MAPS_RECEIVE" />



    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- Requires OpenGL ES version 2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />


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


        <activity
            android:name="org.example.larissaevent.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- Main activity -->
        <activity
            android:name="org.example.larissaevent.MainActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".AllProductsActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".DetailsActivity"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name=".MapActivity"
            android:label="@string/app_name" >
        </activity>


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

    <uses-permission android:name="android.permission.INTERNET" />





</manifest>

任何使其有效的解决方案?我是Android编程新手......

1 个答案:

答案 0 :(得分:0)

你搞砸了碎片和活动。您应该按照以下方式更改代码,它应该可以工作:

public MapFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.map_fragment, container, false);
    Button alarmButton = (Button) view.findViewById(R.id.alarmButton);
    alarmButton.setOnClickListener(this);

    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return view;
}

@Override
public void onDestroyView() {
    MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    if (f != null) {
        try {
            getFragmentManager().beginTransaction().remove(f).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    super.onDestroyView();
}
...
}

然后到你的布局文件:

 <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

希望这有帮助!