2013-02-27 72 views
2

我將Google Cloud Messaging服務添加到我的應用程序並更改了我的清單文件。我得到以下StackTrace。無法實例化活動componentInfo,在清單中聲明的​​活動

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> 

MainActivity:

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 

    } 

} 

任何想法?

+2

'MainActivity'不在'com.example.phptest'包中? – 2013-02-27 19:28:58

+0

請發佈MainActivity並考慮清理您的項目,如果它以'package com.example.phptest;'開頭。 (請在代碼塊中發佈LogCats,因爲引用塊不尊重換行符,因此難以閱讀。) – Sam 2013-02-27 19:33:43

+0

已使用MainActivity更新! – TomSelleck 2013-02-27 20:00:34

回答

0

解決方案是清潔項目,德哦! xD

0

我已經看到Android應用程序在名稱屬性中指定完整路徑時會變得非常挑剔,除非該類所在的包不同於您在清單文件頂部指定的包。嘗試將其改爲「.MainActivity」,看看它是否讓人開心。

+0

沒有骰子的人,謝謝你的回覆 – TomSelleck 2013-02-27 20:16:49

相關問題