在eclipse运行时错误中运行android项目时出错
05-05 14:32:25.210: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.pc/com.org.pc.Login}: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): Caused by: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.org.pc.Login.loginDetails(Login.java:116)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.org.pc.Login.onCreate(Login.java:54)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
代码是:
package com.org.pc;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
public class Login extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Login","Reached");
Log.i("Login","View set");
Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("BitmapImage");
if(bitmap==null)
Log.v("Sdrawable","null");
else
Log.v("Sdrawable","not null");
loginDetails();
setContentView(R.layout.view);
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);
Button login=(Button) findViewById(R.id.login);
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
username = pref.getString(PREF_USERNAME, null);
password = pref.getString(PREF_PASSWORD, null);
if(username != null && password != null)
{
Log.v("username",username);
//user.setText(username);
//pass.setText(password);
}
login.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
setContentView(R.layout.home);
}
});
}
public static final String PREFS_NAME = "PrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
public String username = null;
public String password = null;
//EditText user = (EditText)findViewById(R.id.username);
//EditText pass = (EditText)findViewById(R.id.password);
public void loginDetails()
{
if (username == null || password == null)
{
// username = user.getText().toString();
// password = pass.getText().toString();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://69.10.60.88:88/BZLogin.aspx");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "abdul"));
nameValuePairs.add(new BasicNameValuePair("password", "abdul"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
CheckBox cb = (CheckBox)findViewById(R.id.remember);
if ( cb.isChecked() )
{
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, username)
.putString(PREF_PASSWORD, password)
.commit();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
05-05 14:32:25.210: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.pc/com.org.pc.Login}: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): Caused by: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.org.pc.Login.loginDetails(Login.java:116)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.org.pc.Login.onCreate(Login.java:54)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
the code is :
package com.org.pc;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
public class Login extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Login","Reached");
Log.i("Login","View set");
Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("BitmapImage");
if(bitmap==null)
Log.v("Sdrawable","null");
else
Log.v("Sdrawable","not null");
loginDetails();
setContentView(R.layout.view);
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);
Button login=(Button) findViewById(R.id.login);
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
username = pref.getString(PREF_USERNAME, null);
password = pref.getString(PREF_PASSWORD, null);
if(username != null && password != null)
{
Log.v("username",username);
//user.setText(username);
//pass.setText(password);
}
login.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
setContentView(R.layout.home);
}
});
}
public static final String PREFS_NAME = "PrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
public String username = null;
public String password = null;
//EditText user = (EditText)findViewById(R.id.username);
//EditText pass = (EditText)findViewById(R.id.password);
public void loginDetails()
{
if (username == null || password == null)
{
// username = user.getText().toString();
// password = pass.getText().toString();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://69.10.60.88:88/BZLogin.aspx");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "abdul"));
nameValuePairs.add(new BasicNameValuePair("password", "abdul"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
CheckBox cb = (CheckBox)findViewById(R.id.remember);
if ( cb.isChecked() )
{
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, username)
.putString(PREF_PASSWORD, password)
.commit();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在将布局扩展到活动之前,您正在调用 loginDetails() 。因此,不存在 ui 元素。在调用
findViewById()
之前调用setContentView()
。You're calling loginDetails() before you have inflated your layout to the activity. So, no ui elements exist. call
setContentView()
before you callfindViewById()
.您在
setContentView(R.layout.view)
之前调用了CheckBox cb = (CheckBox)findViewById(R.id.remember)
。这可能就是原因。在
setContentView(R.layout.view)
之后调用loginDetails()
。You called
CheckBox cb = (CheckBox)findViewById(R.id.remember)
beforesetContentView(R.layout.view)
. That's may be why.Call
loginDetails()
aftersetContentView(R.layout.view)
.