2

我正在将 Google Cloud Messaging 服务添加到我的应用程序并更改了我的清单文件。我得到以下 StackTrace。

02-27 18:58:11.282: E/AndroidRuntime(988): FATAL EXCEPTION: main
02-27 18:58:11.282: E/AndroidRuntime(988): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.phptest/com.example.phptest.MainActivity}: java.lang.ClassNotFoundException: com.example.phptest.MainActivity
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.os.Looper.loop(Looper.java:137)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.ActivityThread.main(ActivityThread.java:4745)
02-27 18:58:11.282: E/AndroidRuntime(988):  at java.lang.reflect.Method.invokeNative(Native Method)
02-27 18:58:11.282: E/AndroidRuntime(988):  at java.lang.reflect.Method.invoke(Method.java:511)
02-27 18:58:11.282: E/AndroidRuntime(988):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-27 18:58:11.282: E/AndroidRuntime(988):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 18:58:11.282: E/AndroidRuntime(988):  at dalvik.system.NativeStart.main(Native Method)
02-27 18:58:11.282: E/AndroidRuntime(988): Caused by: java.lang.ClassNotFoundException: com.example.phptest.MainActivity
02-27 18:58:11.282: E/AndroidRuntime(988):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
02-27 18:58:11.282: E/AndroidRuntime(988):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-27 18:58:11.282: E/AndroidRuntime(988):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-27 18:58:11.282: E/AndroidRuntime(988):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-27 18:58:11.282: E/AndroidRuntime(988):  ... 11 more

这是我的清单文件:

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />

    <uses-configuration />

    <permission
        android:name="com.example.phptest.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.phptest.permission.C2D_MESSAGE" />
    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.phptest.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>

        <service android:name=".GCMIntentService" />
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.example.phptest" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

主要活动:

package com.example.phptest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import com.example.phptest.DeviceLogin.LoginReply;
import com.google.android.gcm.GCMRegistrar;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity implements LoginReply {

    String TAG = "Main Activity";

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

        DeviceLogin d = new DeviceLogin(this);
        d.execute("xxxx","12345");

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, GCMIntentService.senderId);
        } else {
          Log.v(TAG, "Already registered");
        }



    }

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


    public static String makeCall(String scriptName) {
        String address = "http://10.0.2.2/" + scriptName + ".php";

        HttpPost httpPost = new HttpPost(address);
        HttpClient httpClient = new DefaultHttpClient();
        StringBuilder total = new StringBuilder();
        try {
            //httpPost.setEntity(new UrlEncodedFormEntity(params));

            // Execute HTTP Post Request
            HttpResponse response = httpClient.execute(httpPost);
            InputStream is = response.getEntity().getContent();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line = "";
            // Read response until the end
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }

            // Return full string
            System.out.println("TOTAL: " + total.toString());

            return total.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "empty";
    }

    public void onDevicesDownloaded(String login) {
        // TODO Auto-generated method stub

    }

}

有任何想法吗?

4

2 回答 2

0

解决方案是清理项目,D'oh!xD

于 2013-02-27T22:52:51.607 回答
0

当在 name 属性中指定完整路径时,我已经看到 Android 应用程序变得很挑剔,除非该类所在的包与您在清单文件顶部指定的包不同。尝试将其更改为“.MainActivity”,看看是否会让人高兴。

于 2013-02-27T20:03:23.390 回答