Soru & Cevap

getText().toString() Sorunu ...

19.05.2016 - 07:22

İnternet üzerindeki bir tutorial yardımıyla mysql bağlantısı yapmaya çalışıyorum fakat kod kısmında bir hata ile karşılaştım. 

String username = user.getText().toString();
String password = pass.getText().toString();

satırlarında yer alan getTex().toString metodları hata veiyor. Kodun tamamı ise:

package com.umbratec.www.xda;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity implements OnClickListener{

    private EditText user, pass;
    private Button  mRegister;



    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    //php login script

    //localhost :
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/register.php";

    //testing on Emulator:
    private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/register.php";

    //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/register.php";

    //ids
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        user = (EditText)findViewById(R.id.username);
        pass = (EditText)findViewById(R.id.password);

        mRegister = (Button)findViewById(R.id.register);
        mRegister.setOnClickListener(this);

    }

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

        new CreateUser().execute();

    }

    class CreateUser extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Register.this);
            pDialog.setMessage("Creating User...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;

            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");

                //Posting user data to script
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);

                // full json response
                Log.d("Login attempt", json.toString());

                // json success element
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("User Created!", json.toString());
                    finish();
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);

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

            return null;

        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null){
                Toast.makeText(Register.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }

}

İlgi ve yardımlarınız için şimdiden teşekkürler.

13 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-28812-1452251417.jpg
umutonur
20.05.2016 - 02:59

Merhaba Furkan..
AsyncTask yapısı, Grafik Arayüzünden (GUI) bağımsız çalışan bir yapıdır.
Yani AsyncTask içinden görsel bileşenlere (Button,EditText,TextView v.s..) direkt olarak ulaşabilmek mümkün değildir..

Bu yüzden göndermek istediğin nesneleri, parametre olarak AsyncTask'a yollarsan; ancak o şekilde sorunu çözebilirsin.

Kod kısmına gelecek olursak : 

new CreateUser.execute();

kodunu

new CreateUser.execute(
        new String[]{
                user.getText().toString(),
                pass.getText().toString()
        });

şeklinde, ve..

String username = user.getText().toString();
String password = pass.getText().toString();

kodunu da 

String username = args[0];
String password = args[1];

şeklinde düzenlersen sorun düzelecektir..

Furkan
20.05.2016 - 06:51
Çok teşekkür ederim. Sorun dediğin gibi düzeldi.
picture-5574-1386508473.jpg
enes.acikoglu
19.05.2016 - 09:19

Selam Furkan,

Hata logunu stacktrace şeklinde hangi satırda aldıgını belirterek tekrar yazarmısın.