选项卡和映射V2,错误:无法实例化活动ComponentInfo

时间:2013-05-23 06:49:38

标签: android

我已经阅读了其他人的问题和答案,我似乎无法找到我的答案,所以我要发布我的main.java活动页面和我的android清单文件,如果你看到的东西我我做错了请告诉我。请记住,日食没有错误;当我尝试通过模拟器运行它时它是不通过的,并且无法在主活动上实例化活动组件的错误显示在我的日志cat上。谢谢!

1)main.java:

package com.arthur.sos;


import android.support.v4.app.Fragment;
import java.util.HashMap;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import com.arthur.sos.R;


public class Main extends FragmentActivity implements TabHost.OnTabChangeListener {

    private TabHost mTabHost;
    private HashMap<String,Object> mapTabInfo = new HashMap<String,Object>();
    private TabInfo mLastTab = null;

    private class TabInfo {
         private String tag;
         @SuppressWarnings("rawtypes")
        private Class clss;
         private Bundle args;
         private android.support.v4.app.Fragment fragment;
         TabInfo(String tag, @SuppressWarnings("rawtypes") Class clazz, Bundle args) {
             this.tag = tag;
             this.clss = clazz;
             this.args = args;
         }

    }

    class TabFactory implements TabContentFactory {

        private final Context mContext;

        /**
         * @param context
         */
        public TabFactory(Context context) {
            mContext = context;
        }

        /** 
         * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
         */
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }

    }
    /** 
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Step 1: Inflate layout
        setContentView(R.layout.tabs_layout);
        // Step 2: Setup TabHost
        initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
    }

    /** 
     * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
     */
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }

    /**
     * Step 2: Setup TabHost
     */
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        //This tab is for Vendor Maps, please note .setIndicator is purposely left blank. 
        Main.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("",getResources().getDrawable(R.drawable.map)), ( tabInfo = new TabInfo("Tab1", vendormaps.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        //This tab is for Reviews, please note .setIndicator is purposely left blank. 
        Main.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("",getResources().getDrawable(R.drawable.reviews)), ( tabInfo = new TabInfo("Tab2", reviews.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        //This tab is for Settings, please note .setIndicator is purposely left blank. 
        Main.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("",getResources().getDrawable(R.drawable.settings)), ( tabInfo = new TabInfo("Tab3", settings.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        //This tab is for My Account, please note .setIndicator is purposely left blank. 
        Main.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("",getResources().getDrawable(R.drawable.myaccount)), ( tabInfo = new TabInfo("Tab4", myaccount.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);


        // Default to first tab
        this.onTabChanged("Tab1");
        //
        mTabHost.setOnTabChangedListener(this);
    }

    /**
     * @param activity
     * @param tabHost
     * @param tabSpec
     * @param clss
     * @param args
     */
    private static void addTab(Main activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }

        tabHost.addTab(tabSpec);
    }

    /** (non-Javadoc)
     * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
     */
    public void onTabChanged(String tag) {
        TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();
        }
    }

}

2)Android Manifest.xml:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17"/>

    <permission
    android:name="com.arthur.sos.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

    <uses-permission android:name="com.arthur.sos.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/rmcsos"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.arthur.sos.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <meta-data
       android:name="com.google.android.maps.v2.API_KEY"
       android:value="certificate area" />
    </application>

</manifest>

3)Activity_Main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >


</RelativeLayout>

4)google地图首先位于XML文件vendormaps.xml:

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

    <selector>
    <!-- When selected -->
    <item android:drawable="@drawable/map"
          android:state_selected="true" />
    <!-- When not selected -->
    <item android:drawable="@drawable/map_inactive"/>
</selector>
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

5)现在活动,vendormaps.java:

package com.arthur.sos;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
//import android.app.Activity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import com.arthur.sos.R;




public class vendormaps extends FragmentActivity {
      static final LatLng Whittier = new LatLng(33.9417909, -117.9861795);
     // static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();


        @SuppressWarnings("unused")
        Marker W1 = map.addMarker(new MarkerOptions().position(Whittier).title("Whittier").snippet("Re/Max").icon(BitmapDescriptorFactory.
                fromResource(R.drawable.rmcsos)));


       // Marker kiel = map.addMarker(new MarkerOptions()
            //.title("Kiel")
            //.snippet("Kiel is cool")
           // .icon(BitmapDescriptorFactory
             //   .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(Whittier, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
      }


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {

            return null;
        }
        return (LinearLayout)inflater.inflate(R.layout.vendormaps, container, false);
    }




}

6)现在logcat错误:

05-16 06:43:15.068: E/AndroidRuntime(2064): FATAL EXCEPTION: main
05-16 06:43:15.068: E/AndroidRuntime(2064): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.arthur.sos/com.arthur.sos.Main}: java.lang.ClassNotFoundException: com.arthur.sos.Main
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.os.Looper.loop(Looper.java:137)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.ActivityThread.main(ActivityThread.java:4340)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at java.lang.reflect.Method.invoke(Method.java:511)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at dalvik.system.NativeStart.main(Native Method)
05-16 06:43:15.068: E/AndroidRuntime(2064): Caused by: java.lang.ClassNotFoundException: com.arthur.sos.Main
05-16 06:43:15.068: E/AndroidRuntime(2064):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-16 06:43:15.068: E/AndroidRuntime(2064):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
05-16 06:43:15.068: E/AndroidRuntime(2064):     ... 11 more

2 个答案:

答案 0 :(得分:0)

您的错误是:

Caused by: java.lang.ClassNotFoundException: com.arthur.sos.Main

因此,在项目中找到Main类时会出现问题。

尝试从以下位置更改清单文件中的活动:

 <activity
        android:name="com.arthur.sos.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

对此:

 <activity
        android:name=".Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

答案 1 :(得分:0)

由于您在应用中使用Gmaps,请确保您的模拟器支持 Google Api ... thr是一个单独的平台,您必须下载以支持Google Apis。所以请确保您拥有该功能。使用支持Google Api作为目标os的平台运行模拟器