我有这个从服务器获取值的登录活动。我能够正确检索值,但我得到一个非法状态异常.-------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- - - - - - - - - - - - - - - - - - - - - 代码:
public class StartActivity extends Activity {
String[] classname= new String[10];
String[] pass= new String[10];
EditText class_name,password;
boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
class_name =(EditText)findViewById(R.id.class_name_et);
password=(EditText)findViewById(R.id.pass_et);
//invalid=(TextView)findViewById(R.id.textView4);
}
public void login(View v)
{
String temp=getServerData(KEY_121);
String un= class_name.getText().toString();
String pas= password.getText().toString();
for(int i=0;i<=3;i++)
{
if(classname[i].equalsIgnoreCase(un)&&pass[i].equalsIgnoreCase(pas))
{
Log.i("success",""+classname[i]);
flag=false;
Intent i1=new Intent(StartActivity.this,Subect_Names.class);
startActivity(i1);
}
}
if(flag)
{
Log.i("fail","failed to login");
}
}
public static final String KEY_121 = "http://192.168.1.7/class_login.php";
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("ClassName","nand"));
nameValuePairs.add(new BasicNameValuePair("Pass","921279"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
classname[i]= json_data.getString("ClassName");
pass[i]= json_data.getString("Pass");
Log.e("test",""+classname[i]);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
} 日志猫:
09-26 18:52:42.294: E/test(328): Rohit
09-26 18:52:42.294: E/test(328): Nanda
09-26 18:52:42.294: E/test(328): abc
09-26 18:52:42.303: I/success(328): Nanda
09-26 18:52:42.323: D/AndroidRuntime(328): Shutting down VM
09-26 18:52:42.323: W/dalvikvm(328): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-26 18:52:42.353: E/AndroidRuntime(328): FATAL EXCEPTION: main
09-26 18:52:42.353: E/AndroidRuntime(328): java.lang.IllegalStateException: Could not execute method of the activity
09-26 18:52:42.353: E/AndroidRuntime(328): at android.view.View$1.onClick(View.java:2072)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.view.View.performClick(View.java:2408)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.view.View$PerformClick.run(View.java:8816)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.os.Handler.handleCallback(Handler.java:587)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.os.Looper.loop(Looper.java:123)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-26 18:52:42.353: E/AndroidRuntime(328): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 18:52:42.353: E/AndroidRuntime(328): at java.lang.reflect.Method.invoke(Method.java:521)
09-26 18:52:42.353: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-26 18:52:42.353: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-26 18:52:42.353: E/AndroidRuntime(328): at dalvik.system.NativeStart.main(Native Method)
09-26 18:52:42.353: E/AndroidRuntime(328): Caused by: java.lang.reflect.InvocationTargetException
09-26 18:52:42.353: E/AndroidRuntime(328): at org.example.attendance.StartActivity.login(StartActivity.java:52)
09-26 18:52:42.353: E/AndroidRuntime(328): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 18:52:42.353: E/AndroidRuntime(328): at java.lang.reflect.Method.invoke(Method.java:521)
09-26 18:52:42.353: E/AndroidRuntime(328): at android.view.View$1.onClick(View.java:2067)
09-26 18:52:42.353: E/AndroidRuntime(328): ... 11 more
09-26 18:52:42.353: E/AndroidRuntime(328): Caused by: java.lang.NullPointerException
09-26 18:52:42.353: E/AndroidRuntime(328): ... 15 more
09-26 18:52:44.523: I/Process(328): Sending signal. PID: 328 SIG: 9