Java 该应用程序已停止运行
我试图用教程编写一个简单的登录页面。这是我的Java页面:Java 该应用程序已停止运行,java,android,xml,Java,Android,Xml,我试图用教程编写一个简单的登录页面。这是我的Java页面: public class Main extends Activity implements OnClickListener{ EditText etUser, etPass; Button bLogin; //Create string variables that will have the input assigned to them String username, password;
public class Main extends Activity implements OnClickListener{
EditText etUser, etPass;
Button bLogin;
//Create string variables that will have the input assigned to them
String username, password;
//Create a HTTPClient as the form container
HttpClient httpclient;
//Use HTTP POST method
HttpPost httppost;
//Create an array list for the input data to be sent
ArrayList<NameValuePair> nameValuePairs;
//Create a HTTP Response and HTTP Entity
HttpResponse response;
HttpEntity entity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialise();
}
private void initialise() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.username);
etPass = (EditText) findViewById(R.id.password);
bLogin = (Button) findViewById(R.id.login_button);
//Now to set an onClickListener
bLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// This is where we will be working now
//Create new default HTTPClient
httpclient = new DefaultHttpClient();
//Create new HTTP POST with URL to php file as parameter
httppost = new HttpPost("http://10.0.2.2:8080/logine/work.php");
//Assign input text to strings
username = etUser.getText().toString();
password = etPass.getText().toString();
//place them in an array list
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
//Next block of code needs to be surrounded by try/catch block for it to work
try {
//Add array list to http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//assign executed form container to response
response = httpclient.execute(httppost);
//check status code, need to check status code 200
if(response.getStatusLine().getStatusCode()== 200){
//assign response entity to http entity
entity = response.getEntity();
//check if entity is not null
if(entity != null){
//Create new input stream with received data assigned
InputStream instream = entity.getContent();
//Create new JSON Object. assign converted data as parameter.
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
//assign json responses to local strings
String retUser = jsonResponse.getString("user");//mySQL table field
String retPass = jsonResponse.getString("pass");
}
}
} catch(Exception e){
e.printStackTrace();
}
}//END onClick()
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();
}//END convertStreamToString()
}
我做错了什么。就算我不太懂java。顺便说一句,我的AVD是2.3.3版的。你没有初始化对象
nameValuePairs
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs=newArrayList();
您没有初始化对象nameValuePairs
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs=newArrayList();
您尚未初始化nameValuePairs
ArrayList<NameValuePair> nameValuePairs;
ArrayList nameValuePairs;
并且它有空值。您还没有初始化nameValuePairs
ArrayList<NameValuePair> nameValuePairs;
ArrayList nameValuePairs;
并且它有空值。-您只声明了
名称值对
类型的对象引用数组列表
变量名称值对
-在类范围
中声明,其和对象
的默认值为null
-您必须初始化它..-您只声明了
名称值对
类型的对象引用数组列表
变量名称值对
-在类范围
中声明,其和对象
的默认值为null
-您必须初始化它..com.Application.Main.onClick(Main.java:79)行是什么?它的nameValuePairs.add(新的BasicNameValuePair(“用户名”,username));添加(新的BasicNameValuePair(“密码”,password));什么是com.Application.Main.onClick(Main.java:79)?它的nameValuePairs.add(新的BasicNameValuePairs(“用户名”,用户名));添加(新的BasicNameValuePair(“密码”,password));我在一开始就初始化了它,我必须再做一次吗?如果必须的话,我应该把它放在哪里?它会获取etuser和et密码,但不会将它们传递到用户名中。你没有像nameValuePairs=new ArrayList()这样的东西;我的用户名仍然是空的。我该怎么办?我在一开始就初始化了它,我必须再做一次吗。如果必须,我应该把它放在哪里?它正在获取etuser和et password,但它不会将它们传递到用户名中。你没有像nameValuePairs=new ArrayList()这样的东西;我的用户名仍为空。我该怎么办?