Android数据库检索 - app不幸停止了

时间:2015-07-20 12:59:12

标签: android database

我正在尝试实现一个简单的用户登录注册页面,但现在我已经实现了用户登录,我的应用程序崩溃了消息:eightmiles is unfortunately stoppedeightmiles是我的包名。

我在此处发布了我的backgroundtask.javasignin.java和我的logcat文件。请帮帮我,告诉我为什么会崩溃我的应用程序。

Backgroundtask.java

    public class Backgroundtask extends AsyncTask<String,Void,String> {  
  AlertDialog alertDialog;  
   Context ctx;  
   Backgroundtask(Context ctx)  
   {  
    this.ctx =ctx;  
   }  
   @Override  
   protected void onPreExecute() {  
   alertDialog = new AlertDialog.Builder(ctx).create();  
     alertDialog.setTitle("Login Information....");  
   }  
   @Override  
   protected String doInBackground(String... params) {  
     String reg_url = "http://8miles.freeiz.com/Register.php";  
     String login_url = "http://8miles.freeiz.com/getuserinfo.php";  
     String method = params[0];  
     if (method.equals("Register")) {  
       String name = params[1];  
       String user_name = params[2];  
       String user_pass = params[3];  
       try {  
         URL url = new URL(reg_url);  
         HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();  
         httpURLConnection.setRequestMethod("POST");  
         httpURLConnection.setDoOutput(true);  
         //httpURLConnection.setDoInput(true);  
         OutputStream OS = httpURLConnection.getOutputStream();  
         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));  
         String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +  
             URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +  
             URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");  
         bufferedWriter.write(data);  
         bufferedWriter.flush();  
         bufferedWriter.close();  
         OS.close();  
         InputStream IS = httpURLConnection.getInputStream();  
         IS.close();  
         //httpURLConnection.connect();  
         httpURLConnection.disconnect();  
         return "Registration Success...";  
       } catch (MalformedURLException e) {  
         e.printStackTrace();  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
     }  
     else if(method.equals("login"))  
     {  
       String login_name = params[1];  
       String login_pass = params[2];  
       try {  
         URL url = new URL(login_url);  
         HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();  
         httpURLConnection.setRequestMethod("POST");  
         httpURLConnection.setDoOutput(true);  
         httpURLConnection.setDoInput(true);  
         OutputStream outputStream = httpURLConnection.getOutputStream();  
         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));  
       String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+  
           URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");  
         bufferedWriter.write(data);  
         bufferedWriter.flush();  
         bufferedWriter.close();  
         outputStream.close();  
         InputStream inputStream = httpURLConnection.getInputStream();  
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));  
         String response = "";  
         String line = "";  
         while ((line = bufferedReader.readLine())!=null)  
         {  
           response+= line;  
         }  
         bufferedReader.close();  
         inputStream.close();  
         httpURLConnection.disconnect();  
         return response;  
       } catch (MalformedURLException e) {  
         e.printStackTrace();  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
     }  
     return null;  
   }  
   @Override  
   protected void onProgressUpdate(Void... values) {  
     super.onProgressUpdate(values);  
   }  
   @Override  
   protected void onPostExecute(String result) {  
    if(result.equals("Registration Success..."))  
    {  
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();  
    }  
     else  
    {  
     alertDialog.setMessage(result);  
      alertDialog.show();  
    }  
   }  
 }  

Signin.java

public class Signin extends Activity {

TextView signuplink;
EditText signusername , signpassword;
Userlocalstore userlocalstore;
Button signin;
String login_name;
String login_pass;

@Override
protected void onCreate(Bundle savedInstanceState) {


    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        signusername = (EditText) findViewById(R.id.susername);
        signpassword = (EditText) findViewById (R.id.spassword);

        userlocalstore = new Userlocalstore(this);
    setContentView(R.layout.signin);

    signin = (Button) findViewById(R.id.signinbutt);
    signin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            userlogin(v);
            //user signedinuser = new user(null,null);
            //userlocalstore.storeuserdata(signedinuser);
            //userlocalstore.setuserloggedin(true);

        }
    });
    signuplink = (TextView) findViewById(R.id.signuplink);
    signuplink.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent signuppage = new Intent ("com.example.eightmiles.SIGNUP");
            startActivity(signuppage);
        }
    });

}

public void userlogin(View view)
{
    login_name = signusername.getText().toString();
    login_pass = signpassword.getText().toString();

    String method = "login";
    Backgroundtask backgroundtask = new Backgroundtask(this);
    backgroundtask.execute(method,login_name,login_pass);
}

}

logcat的

    07-20 08:56:55.992: D/OpenGLRenderer(5848): Render dirty regions requested: true
07-20 08:56:55.997: D/(5848): HostConnection::get() New Host Connection established 0xac1ef9f0, tid 5848
07-20 08:56:56.011: D/Atlas(5848): Validating map...
07-20 08:56:56.047: D/libEGL(5848): loaded /system/lib/egl/libEGL_emulation.so
07-20 08:56:56.048: D/libEGL(5848): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-20 08:56:56.053: D/libEGL(5848): loaded /system/lib/egl/libGLESv2_emulation.so
07-20 08:56:56.074: D/(5848): HostConnection::get() New Host Connection established 0xac1f5260, tid 5864
07-20 08:56:56.095: I/OpenGLRenderer(5848): Initialized EGL, version 1.4
07-20 08:56:56.144: D/OpenGLRenderer(5848): Enabling debug mode 0
07-20 08:56:56.183: W/EGL_emulation(5848): eglSurfaceAttrib not implemented
07-20 08:56:56.183: W/OpenGLRenderer(5848): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa4824480, error=EGL_SUCCESS
07-20 08:56:57.438: W/EGL_emulation(5848): eglSurfaceAttrib not implemented
07-20 08:56:57.438: W/OpenGLRenderer(5848): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa48244a0, error=EGL_SUCCESS
07-20 08:57:02.073: I/Choreographer(5848): Skipped 273 frames!  The application may be doing too much work on its main thread.
07-20 08:57:03.397: W/EGL_emulation(5848): eglSurfaceAttrib not implemented
07-20 08:57:03.397: W/OpenGLRenderer(5848): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa4824480, error=EGL_SUCCESS
07-20 08:57:11.917: D/AndroidRuntime(5848): Shutting down VM
07-20 08:57:11.917: E/AndroidRuntime(5848): FATAL EXCEPTION: main
07-20 08:57:11.917: E/AndroidRuntime(5848): Process: com.example.eightmiles, PID: 5848
07-20 08:57:11.917: E/AndroidRuntime(5848): java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
07-20 08:57:11.917: E/AndroidRuntime(5848):     at com.example.eightmiles.Signin.userlogin(Signin.java:71)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at com.example.eightmiles.Signin$1.onClick(Signin.java:47)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at android.view.View.performClick(View.java:4756)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at android.view.View$PerformClick.run(View.java:19749)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at android.os.Handler.handleCallback(Handler.java:739)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at android.os.Looper.loop(Looper.java:135)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at android.app.ActivityThread.main(ActivityThread.java:5221)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at java.lang.reflect.Method.invoke(Native Method)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at java.lang.reflect.Method.invoke(Method.java:372)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
07-20 08:57:11.917: E/AndroidRuntime(5848):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
07-20 08:57:15.845: I/Process(5848): Sending signal. PID: 5848 SIG: 9
07-20 08:57:16.549: D/OpenGLRenderer(5876): Render dirty regions requested: true
07-20 08:57:16.555: D/(5876): HostConnection::get() New Host Connection established 0xa481b030, tid 5876
07-20 08:57:16.570: D/Atlas(5876): Validating map...
07-20 08:57:16.622: D/libEGL(5876): loaded /system/lib/egl/libEGL_emulation.so
07-20 08:57:16.623: D/libEGL(5876): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-20 08:57:16.633: D/libEGL(5876): loaded /system/lib/egl/libGLESv2_emulation.so
07-20 08:57:16.656: D/(5876): HostConnection::get() New Host Connection established 0xa481b1f0, tid 5891
07-20 08:57:16.682: I/OpenGLRenderer(5876): Initialized EGL, version 1.4
07-20 08:57:16.736: D/OpenGLRenderer(5876): Enabling debug mode 0
07-20 08:57:16.770: W/EGL_emulation(5876): eglSurfaceAttrib not implemented
07-20 08:57:16.770: W/OpenGLRenderer(5876): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa4824340, error=EGL_SUCCESS

1 个答案:

答案 0 :(得分:1)

您应该更改订单

 setContentView(R.layout.signin);
 signusername = (EditText) findViewById(R.id.susername);
 signpassword = (EditText) findViewById (R.id.spassword);

 userlocalstore = new Userlocalstore(this);

首先setContentView(R.layout.signin);,然后初始化观看次数