java.lang.RuntimeException:创建注册表单时执行doInBackground()时发生错误

时间:2016-12-29 06:50:24

标签: android android-asynctask

我想创建注册申请。但我有一个错误。 java.lang.RuntimeException:执行doInBackground()时发生错误 怎么了?这是我的全班

public class signup_activity extends AppCompatActivity {
    EditText username_reg;
    EditText email_reg;
    EditText password_reg;
    Button signup_reg;

    String url_server = "http://192.168.1.215/register.php";

    TextView tanggal_reg;
    Boolean cekinput;
    String aksi;
    String tanggal_kirim;
    HttpResponse response;
    String GetUsername, GetEmail, GetPassword, GetTanggal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        username_reg = (EditText)findViewById(usernameup);
        email_reg = (EditText)findViewById(emailup);
        password_reg = (EditText)findViewById(passwordup);
        signup_reg = (Button)findViewById(signup);
        tanggal_reg = (TextView)findViewById(tanggalup);


        signup_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Getcekinputkosong();
                if(cekinput){
                    kirimdata(GetUsername, GetEmail, GetPassword, GetTanggal);
                }
                else {
                    Toast.makeText(signup_activity.this, "Data yang anda isikan belum lengkap", Toast.LENGTH_LONG). show();
                }
            }
        });
    }

    public void Getcekinputkosong(){
        GetUsername = username_reg.getText().toString();
        GetEmail = email_reg.getText().toString();
        GetPassword = password_reg.getText().toString();
        GetTanggal=tanggal_reg.getText().toString();
        if (TextUtils.isEmpty(GetUsername) || TextUtils.isEmpty(GetEmail) || TextUtils.isEmpty(GetPassword) || TextUtils.isEmpty(GetTanggal)) {
            cekinput = false;
        }
        else {
            cekinput = true;
        }
    }
    public void kirimdata(final String username_reg, final String email_reg, final String password_reg, final String tanggal_reg) {
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                String username_kirim = username_reg;
                String email_kirim = email_reg;
                String password_kirim = password_reg;
                String tanggal_kirim = tanggal_reg;

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username_reg", username_kirim));
                nameValuePairs.add(new BasicNameValuePair("email_reg", email_kirim));
                nameValuePairs.add(new BasicNameValuePair("password_reg", password_kirim));
                nameValuePairs.add(new BasicNameValuePair("tanggal_reg", tanggal_kirim));
                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url_server);
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity entity = response.getEntity();
                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                }
                return "Data Berhasil Terkirim";
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Toast.makeText(signup_activity.this, "Data Berhasil Terkirim", Toast.LENGTH_SHORT).show();
            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(username_reg, email_reg, password_reg);
    }

    public void kembali_login(View arg0){
        Intent kembali = new Intent(signup_activity.this, register_activity.class);
        startActivity(kembali);
        signup_activity.this.finish();
    }


    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new datepickerlah();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

}

我在运行时遇到此错误:

  

E / AndroidRuntime:       致命异常:AsyncTask#3
      处理:com.example.client18.dd,PID:8356
      java.lang.RuntimeException:执行时发生错误       doInBackground()
      在android.os.AsyncTask $ 3.done(AsyncTask.java:300)
      在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)

at java.util.concurrent.FutureTask.setException(FutureTask.java:222)     

at java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                            
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)                                                                           
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.NullPointerException                                        

at com.example.client18.dd.signup_activity$1SendPostReqAsyncTask.doInBackground(signup_activity.java:128)

at com.example.client18.dd.signup_activity$1SendPostReqAsyncTask.doInBackground(signup_activity.java:112)

at android.os.AsyncTask$2.call(AsyncTask.java:288)                                                                           
at java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                            
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)                                                                             
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 

at java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:0)

这是我的register.php

<?php
$server = "localhost";
$user="root";
$password="";
$database = "contohdatabase";
$koneksi = mysql_connect($server, $user, $password, $database);
 if(isset($_POST['username']) && isset($_POST['password'])){
$nama = $_POST['username_reg'];
$email = $_POST['email_reg'];
$password = $_POST['password_reg'];
$tanggal_lahir = $_POST['tanggal_reg'];
$tambahdata = "INSERT INTO login(username, email, password, tanggal_lahir) VALUE($nama, $email, $password, $tanggal_lahir);";
if(mysql_query($koneksi,$tambahdata)){
echo "Data berhasil diinput ";
}
else {
echo "Data tidak sukses terkirim";
}
echo "$result";
}
?> 

答案 1 :(得分:0)

你的错误在这里,

 HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();

您尚未初始化&#34;响应&#34;并使用它。所以,用&#34; httpResponse&#34;。

替换它