我收到一条错误消息:RuntimeException:无法实例化活动-但我不确定为什么会发生这种情况。任何建议,不胜感激。
尝试执行以下源代码时:
package com.example.httpgetandroidexample;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import com.androidexample.httpgetexample.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class HttpGetAndroidExample<AsyncronoustaskAndroidExample> extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button GetServerData = (Button) findViewById(R.id.GetServerData);
GetServerData.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Server Request URL
String serverURL = "http://androidexample.com/media/webservice/getPage.php";
// Create Object and call AsyncTask execute Method
new LongOperation().execute(serverURL);
}
});
}
// Class with extends AsyncTask class
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(HttpGetAndroidExample.this);
TextView uiUpdate = (TextView) findViewById(R.id.output);
protected void onPreExecute() {
// NOTE: You can call UI Element here.
//UI Element
uiUpdate.setText("Output : ");
Dialog.setMessage("Downloading source..");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
try {
// Call long running operations here (perform background computation)
// NOTE: Don't call UI Element here.
// Server url call by GET method
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
// Close progress dialog
Dialog.dismiss();
if (Error != null) {
uiUpdate.setText("Output : "+Error);
} else {
uiUpdate.setText("Output : "+Content);
}
}
}
}
07-16 11:48:37.622: D/ActivityThread(21370): setTargetHeapUtilization:0.25
07-16 11:48:37.622: D/ActivityThread(21370): setTargetHeapIdealFree:8388608
07-16 11:48:37.622: D/ActivityThread(21370): setTargetHeapConcurrentStart:2097152
07-16 11:48:37.642: W/dalvikvm(21370): threadid=1: thread exiting with uncaught exception (group=0x41ba7438)
07-16 11:48:37.652: E/AndroidRuntime(21370): FATAL EXCEPTION: main
07-16 11:48:37.652: E/AndroidRuntime(21370): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidexample.httpgetexample/com.androidexample.httpgetexample.HttpGetAndroidExample}: java.lang.ClassNotFoundException: com.androidexample.httpgetexample.HttpGetAndroidExample
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.ActivityThread.access$700(ActivityThread.java:139)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.os.Looper.loop(Looper.java:137)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.ActivityThread.main(ActivityThread.java:4918)
07-16 11:48:37.652: E/AndroidRuntime(21370): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 11:48:37.652: E/AndroidRuntime(21370): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 11:48:37.652: E/AndroidRuntime(21370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-16 11:48:37.652: E/AndroidRuntime(21370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-16 11:48:37.652: E/AndroidRuntime(21370): at dalvik.system.NativeStart.main(Native Method)
07-16 11:48:37.652: E/AndroidRuntime(21370): Caused by: java.lang.ClassNotFoundException: com.androidexample.httpgetexample.HttpGetAndroidExample
07-16 11:48:37.652: E/AndroidRuntime(21370): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-16 11:48:37.652: E/AndroidRuntime(21370): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-16 11:48:37.652: E/AndroidRuntime(21370): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
07-16 11:48:37.652: E/AndroidRuntime(21370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2003)
07-16 11:48:37.652: E/AndroidRuntime(21370): ... 11 more
最佳答案
您在清单文件中的活动的类名称错误,它使用了错误的程序包名称。
似乎正在使用:
com.androidexample.httpgetexample.HttpGetAndroidExample
而你的班级是
com.example.httpgetandroidexample.HttpGetAndroidExample