不幸的是应用已停止

时间:2015-12-02 12:28:47

标签: android apache crash

我正在尝试创建一个登录注册应用程序但是当我打开注册活动时它会崩溃。 我似乎无法弄清楚我在https://www.youtube.com/watch?v=M4VTi5-Aw20给出的教程系列中遵循相同步骤的问题是什么 以下是我的代码: Register.java: 包com.mohd.loginre;

    import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.ActionBarActivity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;


        public class Register extends ActionBarActivity implements View.OnClickListener {

        EditText user=(EditText)findViewById(R.id.rusername);
        EditText pass=(EditText)findViewById(R.id.rpassword);
        EditText ag=(EditText)findViewById(R.id.rage);
        Button rsubmit;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            rsubmit=(Button)findViewById(R.id.rsubmit);
            rsubmit.setOnClickListener( this);
        }


        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.rsubmit){
                String username=user.getText().toString();
                String password=pass.getText().toString();
                int age=Integer.parseInt(ag.getText().toString());
                User user=new User(username,password,age);
                registerhim(user);
            }
        }

        private void registerhim(User user) {
            ServerRequests serverRequest=new ServerRequests(this);
            serverRequest.storeUserDataInBackground(user, new GetUserCallback() {
                @Override
                public void done(User returnedUser) {
                    Intent loginIntent = new Intent(Register.this, Login.class);
                    startActivity(loginIntent);
                }
            });


        }
    }




    ServerRequests.java

    package com.mohd.loginre;

    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.util.Log;

    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.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONObject;

    import java.util.ArrayList;

    public class ServerRequests {
        ProgressDialog progressDialog;
        public static final int Timeout = 1000 * 15;
        public static final String server = "http://mohdgadi.netai.net/";
    public ServerRequests(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setTitle("Processing...");
        progressDialog.setMessage("Please wait...");
    }

    public void storeUserDataInBackground(User user,
                                          GetUserCallback userCallBack) {
        progressDialog.show();
        new StoreUserDataAsyncTask(user, userCallBack).execute();
    }

    public void fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
        progressDialog.show();
        new fetchUserDataAsyncTask(user, userCallBack).execute();
    }

    /**
     * parameter sent to task upon execution progress published during
     * background computation result of the background computation
     */

    public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
        User user;
        GetUserCallback userCallBack;

        public StoreUserDataAsyncTask(User user, GetUserCallback userCallBack) {
            this.user = user;
            this.userCallBack = userCallBack;
        }

        @Override
        protected Void doInBackground(Void... params) {
            ArrayList<NameValuePair> dataToSend = new ArrayList<>();
            dataToSend.add(new BasicNameValuePair("username", user.username));
            dataToSend.add(new BasicNameValuePair("password", user.password));
            dataToSend.add(new BasicNameValuePair("age", user.age + ""));

            HttpParams httpRequestParams = getHttpRequestParams();

            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(server
                    + "Register.php");

            try {
                post.setEntity(new UrlEncodedFormEntity(dataToSend));
                client.execute(post);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        private HttpParams getHttpRequestParams() {
            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                   Timeout);
            HttpConnectionParams.setSoTimeout(httpRequestParams,
                    Timeout);
            return httpRequestParams;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
            userCallBack.done(null);
        }

    }

    public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
        User user;
        GetUserCallback userCallBack;

        public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
            this.user = user;
            this.userCallBack = userCallBack;
        }

        @Override
        protected User doInBackground(Void... params) {
            ArrayList<NameValuePair> dataToSend = new ArrayList<>();
            dataToSend.add(new BasicNameValuePair("username", user.username));
            dataToSend.add(new BasicNameValuePair("password", user.password));

            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                    Timeout);
            HttpConnectionParams.setSoTimeout(httpRequestParams,
                    Timeout);

            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(server
                    + "FetchUserData.php");

            User returnedUser = null;

            try {
                post.setEntity(new UrlEncodedFormEntity(dataToSend));
                HttpResponse httpResponse = client.execute(post);

                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                JSONObject jObject = new JSONObject(result);

                if (jObject.length() != 0){
                    Log.v("happened", "2");
                    String name = jObject.getString("name");
                    int age = jObject.getInt("age");

                    returnedUser = new User(user.username,
                            user.password,age);
                }

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

            return returnedUser;
        }

        @Override
        protected void onPostExecute(User returnedUser) {
            super.onPostExecute(returnedUser);
            progressDialog.dismiss();
            userCallBack.done(returnedUser);
        }
    }
}

我在编译期间没有遇到任何错误我在模拟器上尝试了这个以及我的Android设备但是同样的错误。请帮助!!

2 个答案:

答案 0 :(得分:2)

移动

EditText user=(EditText)findViewById(R.id.rusername);
EditText pass=(EditText)findViewById(R.id.rpassword);
EditText ag=(EditText)findViewById(R.id.rage);
onCreate(..)

之后的setContentView(R.layout.activity_register);

答案 1 :(得分:1)

这样做:

 Button rsubmit;
 EditText user,pass,ag;

     @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_register);
                rsubmit=(Button)findViewById(R.id.rsubmit);

                user=(EditText)findViewById(R.id.rusername);
                pass=(EditText)findViewById(R.id.rpassword);
                ag=(EditText)findViewById(R.id.rage);


                rsubmit.setOnClickListener( this);
            }