2011-05-26 35 views
0

可能重複Android的錯誤:
Sending A string of lat and long from mobile to server在發送數據代碼

enter code here 
package andpy.ap; 

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

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.client.ClientProtocolException; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.methods.HttpGet; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

     import android.util.Log; 

     public class andpy { 

private static String convertStreamToString(InputStream is) { 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

/* This is a test function which will connects to a given 
* rest service and prints it's response to Android Log with 
* labels "Praeda". 
*/ 
public static void connect(String url) 
{ 

    HttpClient httpclient = new DefaultHttpClient(); 

    // Prepare a request object 
    HttpGet httpget = new HttpGet(url); 

    // Execute the request 
    HttpResponse response; 
    try { 
     response = httpclient.execute(httpget); 
     // Examine the response status 
     Log.i("Praeda",response.getStatusLine().toString()); 

     // Get hold of the response entity 
     HttpEntity entity = response.getEntity(); 
     // If the response does not enclose an entity, there is no need 
     // to worry about connection release 

     if (entity != null) { 

      // A Simple JSON Response Read 
      InputStream instream = entity.getContent(); 
      String result= convertStreamToString(instream); 
      Log.i("Praeda",result); 

      // A Simple JSONObject Creation 
      JSONObject json=new JSONObject(result); 
      Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>"); 

      // A Simple JSONObject Parsing 
      JSONArray nameArray=json.names(); 
      JSONArray valArray=json.toJSONArray(nameArray); 
      for(int i=0;i<valArray.length();i++) 
      { 
       Log.i("Praeda","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n" 
         +"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">"); 
      } 

      // A Simple JSONObject Value Pushing 
      json.put("sample key", "sample value"); 
      Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>"); 

      // Closing the input stream will trigger connection release 
      instream.close(); 
     } 


    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    } 

日誌貓

enter code here 
05-27 01:42:07.728: ERROR/AndroidRuntime(196): Uncaught handler: thread main exiting due to uncaught exception 
05-27 01:42:07.798: ERROR/AndroidRuntime(196): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{andpy.ap/andpy.ap.andpy}: java.lang.ClassCastException: andpy.ap.andpy 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.os.Looper.loop(Looper.java:123) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.ActivityThread.main(ActivityThread.java:4338) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at dalvik.system.NativeStart.main(Native Method) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196): Caused by: java.lang.ClassCastException: andpy.ap.andpy 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394) 
05-27 01:42:07.798: ERROR/AndroidRuntime(196):  ... 11 more 

讓我在做什麼錯誤的錯誤代碼?!!

+0

您的代碼不包含任何活動開始。該錯誤可能是由於開始活動的問題而發生的。如果你發佈更多的代碼,可能會更容易給你一個答案。 – 2011-05-26 23:07:49

+0

它只是這個代碼 – cero 2011-05-26 23:19:33

+0

「這樣的錯誤是什麼我做錯了代碼?!!」花一點時間再讀一遍,重寫它,這樣纔有意義,而且英文很好。 – 2011-05-27 05:58:03

回答

0

根據我在評論中的問題的答案,我認爲您將AndroidManifest.xml中的andpy.ap.andpy類作爲活動。

但是,它與Activity無關。您必須擴展Activity並使用適當的協議來啓動應用程序(onCreate()等)。請再次查看文檔。

簡而言之,Android期望一個活動並嘗試將您的類轉換爲它。

/** 
* Perform instantiation of an {@link Activity} object. This method is intended for use with 
* unit tests, such as android.test.ActivityUnitTestCase. The activity will be useable 
* locally but will be missing some of the linkages necessary for use within the sytem. 
* 
* @param clazz The Class of the desired Activity 
* @param context The base context for the activity to use 
* @param token The token for this activity to communicate with 
* @param application The application object (if any) 
* @param intent The intent that started this Activity 
* @param info ActivityInfo from the manifest 
* @param title The title, typically retrieved from the ActivityInfo record 
* @param parent The parent Activity (if any) 
* @param id The embedded Id (if any) 
* @param lastNonConfigurationInstance Arbitrary object that will be 
* available via {@link Activity#getLastNonConfigurationInstance() 
* Activity.getLastNonConfigurationInstance()}. 
* @return Returns the instantiated activity 
* @throws InstantiationException 
* @throws IllegalAccessException 
*/ 
public Activity newActivity(Class<?> clazz, Context context, 
     IBinder token, Application application, Intent intent, ActivityInfo info, 
     CharSequence title, Activity parent, String id, 
     Object lastNonConfigurationInstance) throws InstantiationException, 
     IllegalAccessException { 
    Activity activity = (Activity)clazz.newInstance(); 
    ActivityThread aThread = null; 
    activity.attach(context, aThread, this, token, application, intent, info, title, 
      parent, id, lastNonConfigurationInstance, new Configuration()); 
    return activity; 
} 
相關問題