过去两天我面临一个问题,但没有任何帮助。请有人看看我的代码,分析它并指出我的错误。(我是 Android BTW 的初学者)。
错误日志:
12-28 16:09:02.087: E/AndroidRuntime(366): FATAL EXCEPTION: main
12-28 16:09:02.087: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{admin.HazuriNet.hukam/admin.HazuriNet.hukam.HazuriNet_AdminActivity}: java.lang.NullPointerException
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.os.Looper.loop(Looper.java:123)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 16:09:02.087: E/AndroidRuntime(366): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:09:02.087: E/AndroidRuntime(366): at java.lang.reflect.Method.invoke(Method.java:521)
12-28 16:09:02.087: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-28 16:09:02.087: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-28 16:09:02.087: E/AndroidRuntime(366): at dalvik.system.NativeStart.main(Native Method)
12-28 16:09:02.087: E/AndroidRuntime(366): Caused by: java.lang.NullPointerException
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.Activity.findViewById(Activity.java:1637)
12-28 16:09:02.087: E/AndroidRuntime(366): at admin.HazuriNet.hukam.HazuriNet_AdminActivity.<init>(HazuriNet_AdminActivity.java:81)
12-28 16:09:02.087: E/AndroidRuntime(366): at java.lang.Class.newInstanceImpl(Native Method)
12-28 16:09:02.087: E/AndroidRuntime(366): at java.lang.Class.newInstance(Class.java:1429)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 16:09:02.087: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
12-28 16:09:02.087: E/AndroidRuntime(366): ... 11 more
和活动:
package admin.HazuriNet.hukam;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
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;
public class HazuriNet_AdminActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
postData();
}
});
}
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.hazurinet.com/set/hukum_class.php");
//This is the data to send
String UserName = txtUserName.getText().toString(); //any data to send
String Password = txtPassword.getText().toString();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("app", "zizzle"));
nameValuePairs.add(new BasicNameValuePair("un", UserName));
nameValuePairs.add(new BasicNameValuePair("pwd", Password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
//This is the response from a php application
lblResponce.setText(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}//end postData()
/** Declarations */
EditText txtUserName = (EditText) findViewById(R.id.txtUserName);
EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
TextView lblResponce = (TextView) findViewById(R.id.lblResponce);
}
和清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="admin.HazuriNet.hukam"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:permission="android.permission.INTERNET">
<activity
android:name=".HazuriNet_AdminActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果您想了解更多信息,请告诉我...提前致谢...