google play services无法实例化活动ComponentInfo java.lang.ClassNotFoundException

时间:2013-12-05 07:03:06

标签: java android android-activity google-play-games

在我导入google play服务和basegamesutils之前一切正常。现在app崩溃了,在logcat中我可以看到:

    12-05 07:42:49.445: E/AndroidRuntime(6223): FATAL EXCEPTION: main
12-05 07:42:49.445: E/AndroidRuntime(6223): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kubasienki.freefall/com.kubasienki.freefall.MainActivity}: java.lang.ClassNotFoundException: com.kubasienki.freefall.MainActivity
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.os.Looper.loop(Looper.java:137)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.ActivityThread.main(ActivityThread.java:4898)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at dalvik.system.NativeStart.main(Native Method)
12-05 07:42:49.445: E/AndroidRuntime(6223): Caused by: java.lang.ClassNotFoundException: com.kubasienki.freefall.MainActivity
12-05 07:42:49.445: E/AndroidRuntime(6223):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
12-05 07:42:49.445: E/AndroidRuntime(6223):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
12-05 07:42:49.445: E/AndroidRuntime(6223):     ... 11 more

我的MainActivity.java:

package com.kubasienki.freefall;

import com.google.example.games.basegameutils.BaseGameActivity;
import com.kubasienki.freefall.R;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BaseGameActivity
implements View.OnClickListener {





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

    //  final Button button = (Button) findViewById(R.id.start);
      //  button.setOnClickListener(new View.OnClickListener() {
            //@Override
            //public void onClick(View v) {
         //     Intent intent = new Intent(MainActivity.this, Fall.class);

       //         startActivity(intent);      

     //       }
   //     });

        //final Button button1 = (Button) findViewById(R.id.howto);
    //    button1.setOnClickListener(new View.OnClickListener() {
      //      @Override
    //      public void onClick(View v) {
  //            Intent intent1 = new Intent(MainActivity.this, HowTo.class);
//
           //     startActivity(intent1);      

         //   }
       // });
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);
        findViewById(R.id.howto).setOnClickListener(this);
        findViewById(R.id.start).setOnClickListener(this);



    }

    @Override
    public void onSignInFailed() {
        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_button).setVisibility(View.GONE);


    }

    @Override
    public void onSignInSucceeded() {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);


    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.sign_in_button) {
            // start the asynchronous sign in flow
            beginUserInitiatedSignIn();
        }
        else if (view.getId() == R.id.sign_out_button) {
            // sign out.
            signOut();

            // show sign-in button, hide the sign-out button
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            findViewById(R.id.sign_out_button).setVisibility(View.GONE);
        }
        else if(view.getId() == R.id.howto){
            Intent intent1 = new Intent(MainActivity.this, HowTo.class);

            startActivity(intent1); 
        }
        else if(view.getId() == R.id.start){
            Intent intent = new Intent(MainActivity.this, Fall.class);

            startActivity(intent);}
    }

    //@Override
    //public boolean onCreateOptionsMenu(Menu menu) {
    //  // Inflate the menu; this adds items to the action bar if it is present.
    //  getMenuInflater().inflate(R.menu.main, menu);
    //  return true;
    //}

}

和清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

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

        <activity
            android:name="com.kubasienki.freefall.MainActivity"
            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="com.kubasienki.freefall.Fall"
        android:label="@string/app_name"
        android:parentActivityName="com.kubasienki.freefall.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.kubasienki.freefall.MainActivity" />
    </activity>
    <activity
        android:name="com.kubasienki.freefall.HowTo"
        android:label="@string/app_name"
        android:parentActivityName="com.kubasienki.freefall.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.kubasienki.freefall.MainActivity" />
    </activity>
           <meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />
    </application>

</manifest>

我检查了什么:

  1. 导入了google-play-services_lib和BaseGameUtils
  2. 检查了构建路径

4 个答案:

答案 0 :(得分:1)

对我有什么帮助: 1.在教程中是在ids.xml中注册app_id然后他们使用app_id表单文件strings.xml。 我添加了:

<meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

到Manifest.xml

答案 1 :(得分:0)

试试这个:

转到项目/属性/ Java构建路径/订单和导出 - 确保在Android Dependencies和支持库前面有一个检查,如果您使用它。标记所有复选框。单击应用并清理项目。

希望这有帮助。

答案 2 :(得分:0)

确保在Android Private Libraries中选中Order and Export。 如果未选中,check it and restart eclipse。

答案 3 :(得分:0)

这是因为 MainActivity 派生自 BaseGameActivity ,而类加载器找不到 BaseGameActivity ,因为它未包含在APK中。要解决此构建问题,请确保正确构建 BaseGameUtils 库并将其包含在APK中。在Eclipse中,您需要右键单击 BaseGameUtils 项目并转到项目属性,并确保它被标记为库(有一个复选框)。然后,转到项目的属性,并确保将 BaseGameUtils 项目列为其依赖项。如果没有,请添加它。