过去两天我遇到了一个问题,但我没有任何帮助。请有人看看我的代码,分析它并指出我的错误。 (我是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);
}
和Manifest:
<?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>
如果您想要更多信息,请告知我... 在此先感谢...
答案 0 :(得分:0)
首先使用findViewById初始化btnLogin
,然后再使用它。
Button btnLogin = (Button)findViewById(R.id.yourButtonId);
编辑: - 在onCreate()方法中声明您的EditText,Button和Textview,如下面的代码。
public class HazuriNet_AdminActivity extends Activity
{
EditText txtUserName,txtPassword;
Button btnLogin ;
TextView lblResponce;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName = (EditText) findViewById(R.id.txtUserName);
txtPassword = (EditText) findViewById(R.id.txtPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
lblResponce = (TextView) findViewById(R.id.lblResponce);
btnLogin.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
postData();
}
});
}
// Remaining Code
}
答案 1 :(得分:0)
问题在于button
。在btnLogin
onClickListener
之前,先初始化按钮onCreate()
现在您正在初始化PostData()
上的按钮,该按钮在调用btnLogin.onClickListener()
时未被调用
public class HazuriNet_AdminActivity extends Activity {
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
TextView lblResponce;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Declarations */
txtUserName = (EditText) findViewById(R.id.txtUserName);
txtPassword = (EditText) findViewById(R.id.txtPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
lblResponce = (TextView) findViewById(R.id.lblResponce);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
postData();
}
});
}
}
答案 2 :(得分:0)
执行此声明:
/** 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);
在
之后setContentView(R.layout.main);
感谢。
答案 3 :(得分:0)
你没有初始化btnLogin
按钮只是像这样创建,
按钮btnLogin =(按钮)findviewbyid(R.id.btnLogin);
答案 4 :(得分:0)
在活动的setContentView(R.layout.main);
方法中onCreate()
之后写下以下代码行,并从postdata()
方法中删除。
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);
它将解决您的问题。