强制关闭申请

时间:2012-04-15 18:22:20

标签: android json jsp android-layout

当我点击登录按钮时,我的申请人关闭......为什么会这样?这是什么解决方案?

我的Login_Menu活动如下: -

package com.campuspro.start;

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.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 org.json.JSONException;
import org.json.JSONObject;

 import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Login_Menu extends Activity {

EditText usname;
EditText pass;
TextView tv;
HttpClient client;
HttpPost post;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_lay);
 tv=(TextView) findViewById(R.id.login_stat_tv);
 usname=(EditText)findViewById(R.id.uname);
 pass=(EditText)findViewById(R.id.pass);
Button login=(Button)findViewById(R.id.login_but);
Button cancel=(Button)findViewById(R.id.cancel_but);

client = new DefaultHttpClient();
String url="http://10.0.2.2:7001/proj/login.jsp";
post = new HttpPost(url);
login.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        new login().execute("");
    }
});

cancel.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        usname.getText().clear();
        pass.getText().clear();
    }
});

 }


 private class login extends AsyncTask<String, Void, JSONObject>{
 ProgressDialog dialog = ProgressDialog.show(Login_Menu.this, "", "Authenticating,    Please wait...");

  @Override
  protected JSONObject doInBackground(String... params) {
    // TODO Auto-generated method stub
    Log.i("thread", "Doing Something...");
   //authentication operation
try{

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();   
    pairs.add(new BasicNameValuePair("username",usname.getText().toString()));   
    pairs.add(new BasicNameValuePair("password",pass.getText().toString()));   
    post.setEntity(new UrlEncodedFormEntity(pairs));   
    HttpResponse response = client.execute(post);
    int status=response.getStatusLine().getStatusCode();

    if(status == 200)
    {
        HttpEntity e=response.getEntity();
        String data=EntityUtils.toString(e);
        JSONObject last=new JSONObject(data);
        return last;

    }

}

 catch(Exception e)
 {
     e.printStackTrace();   

 }

    return null;
 }

 protected void onPreExecute(){
    //dialog.dismiss();
    Log.i("thread", "Started...");
    dialog.show();
 }
 protected void onPostExecute(JSONObject result){
    Log.i("thread", "Done...");
    String name;
    try {
        name = result.getString("status");

    if(dialog!=null)
    dialog.dismiss();
    if(name.equalsIgnoreCase("yes")){
        tv.setText("user found");
        Intent myIntent=new Intent("com.campuspro.start.MYMENU");
        startActivity(myIntent);

    }else{

    tv.setText("No User Found, please try again!");
    }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }

}}

我的login.jsp页面如下......

 <%@page contentType="text/html; charset=UTF-8"%>
 <%@page import="org.json.simple.JSONObject"%>
 <%@page import="java.util.*,java.sql.*"%>

 <%!

 Connection con;
 PreparedStatement ps;
 ResultSet rs;
 String x,y;

 %>

 <%
 Class.forName("oracle.jdbc.driver.OracleDriver");
 con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","pro","pro");
 x=request.getParameter("username");
 y=request.getParameter("password");

 ps=con.prepareStatement("select * from login_stud where uname=? and pass=?");
 ps.setString(1,x);
 ps.setString(2,y);
 rs=ps.executeQuery();

JSONObject obj=new JSONObject();
if(rs.next())
{
obj.put("status","yes");
out.print(obj);

}
else
{
obj.put("status","no");
out.print(obj);
}
out.flush();
 %>

这是我的日志猫......

 04-15 23:42:06.449: I/thread(276): Started...
 04-15 23:42:06.460: I/thread(276): Doing Something...
 04-15 23:42:07.950: D/dalvikvm(276): GC_FOR_MALLOC freed 3284 objects / 170544 bytes   in 156ms
 04-15 23:42:10.469: I/thread(276): Done...
 04-15 23:42:10.469: D/AndroidRuntime(276): Shutting down VM
 04-15 23:42:10.469: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
 04-15 23:42:10.509: E/AndroidRuntime(276): FATAL EXCEPTION: main
 04-15 23:42:10.509: E/AndroidRuntime(276): java.lang.NullPointerException
 04-15 23:42:10.509: E/AndroidRuntime(276):     at    com.campuspro.start.Login_Menu$login.onPostExecute(Login_Menu.java:117)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at      com.campuspro.start.Login_Menu$login.onPostExecute(Login_Menu.java:1)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at android.os.AsyncTask.finish(AsyncTask.java:417)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at android.os.Handler.dispatchMessage(Handler.java:99)
  04-15 23:42:10.509: E/AndroidRuntime(276):    at android.os.Looper.loop(Looper.java:123)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at java.lang.reflect.Method.invokeNative(Native Method)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at java.lang.reflect.Method.invoke(Method.java:521)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 04-15 23:42:10.509: E/AndroidRuntime(276):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

这是根本原因:

Intent myIntent=new Intent("com.campuspro.start.MYMENU");
startActivity(myIntent);

应该是这样的:

Intent myIntent=new Intent(Login_Menu.this, MYMENU.class);
startActivity(myIntent);

答案 1 :(得分:0)

应用程序强制关闭,因为Android清单文件中未提及MYMENU活动。

启动意图时没有出现这样的错误。

在androidmanifest.xml中声明活动的过程是:

<activity
        android:name=".ActivityName"
        android:label="@string/app_name"
         >
        <intent-filter>
            <action android:name="com.campuspro.start.ACTIVITYNAME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

谢谢