当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Error in getting text from edittext-Android

问题描述:

I am creating an application which will take input from the user and send it to server for searching. Here are my two problems

  1. Error in getting error from the edittext
  2. Toast is not working

here is my code

 public class QuickSearch extends Activity {

Button search;

RadioGroup stones;

RadioButton single,twin,selected_value;

EditText to,from,unique_id;

TextView tv;

HttpPost httppost;

StringBuffer buffer;

HttpResponse response;

HttpClient httpclient;

List<NameValuePair> nameValuePairs;

ProgressDialog dialog = null;

Intent f1;

String radiovalue;

String str_color1,str_color2,str_clarity1, str_clarity2, str_shape1,str_shape2, str_cut1,str_cut2,str_carat1,str_carat2;

Spinner color1,color2,shape1,shape2,clarity1,clarity2,cut,cut2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_quicksearch);

search=(Button)findViewById(R.id.search);

// submit=(Button)findViewById(R.id.submit);

to=(EditText)findViewById(R.id.to);

from=(EditText)findViewById(R.id.from);

// unique_id=(EditText)findViewById(R.id.unique_id);

tv = (TextView)findViewById(R.id.tv);

stones=(RadioGroup)findViewById(R.id.radioGroup);

single=(RadioButton)findViewById(R.id.single);

twin=(RadioButton)findViewById(R.id.twin);

f1 = new Intent(QuickSearch.this,Search_result.class);

color1=(Spinner)findViewById(R.id.color1);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.color1, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

color1.setAdapter(adapter);

color2=(Spinner)findViewById(R.id.color2);

ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.color2, android.R.layout.simple_spinner_item);

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

color2.setAdapter(adapter2);

shape1=(Spinner)findViewById(R.id.shape);

ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.shape, android.R.layout.simple_spinner_item);

adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

shape1.setAdapter(adapter3);

shape2=(Spinner)findViewById(R.id.shape2);

ArrayAdapter<CharSequence> adapter8 = ArrayAdapter.createFromResource(this, R.array.shape2, android.R.layout.simple_spinner_item);

adapter8.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

shape2.setAdapter(adapter8);

clarity1=(Spinner)findViewById(R.id.clarity1);

ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this,

R.array.clarity1, android.R.layout.simple_spinner_item);

adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

clarity1.setAdapter(adapter4);

clarity2=(Spinner)findViewById(R.id.clarity2);

ArrayAdapter<CharSequence> adapter5 = ArrayAdapter.createFromResource(this,R.array.clarity2, android.R.layout.simple_spinner_item);

adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

clarity2.setAdapter(adapter5);

cut=(Spinner)findViewById(R.id.cut);

ArrayAdapter<CharSequence> adapter6 = ArrayAdapter.createFromResource(this,R.array.cut, android.R.layout.simple_spinner_item);

adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

cut.setAdapter(adapter6);

cut2=(Spinner)findViewById(R.id.cut2);

ArrayAdapter<CharSequence> adapter7 = ArrayAdapter.createFromResource(this,R.array.cut2, android.R.layout.simple_spinner_item);

adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

cut2.setAdapter(adapter7);

/*

if(stones.getCheckedRadioButtonId()!=-1){

int id= stones.getCheckedRadioButtonId();

View radioButton = stones.findViewById(id);

// int radioId = radioGroup.indexOfChild(radioButton);

RadioButton btn = (RadioButton) stones.getChildAt(radioId);

radiovalue = (String) btn.getText();

}

*/

//--------------------------------getting value on button press------------------

search.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

str_color1= color1.getSelectedItem().toString();

str_color2= color2.getSelectedItem().toString();

str_shape1=shape1.getSelectedItem().toString();

str_shape2=shape2.getSelectedItem().toString();

str_clarity2=clarity2.getSelectedItem().toString();

str_clarity1= clarity1.getSelectedItem().toString();

str_cut1=cut.getSelectedItem().toString();

str_cut2=cut2.getSelectedItem().toString();

str_carat1=to.getText().toString().trim();

str_carat2=from.getText().toString().trim();

dialog = ProgressDialog.show(QuickSearch.this, "","Searching...", true);

int selectedId = stones.getCheckedRadioButtonId();

// find the radiobutton by returned id

selected_value = (RadioButton) findViewById(selectedId);

radiovalue= selected_value.getText().toString();

if(str_color1=="Color To:" || str_color2=="Color From:" || str_clarity1=="Color To:" || str_clarity2 =="Color From:"

|| str_cut1 == "Cut To:" || str_cut2 == " Cut From:" || str_shape1=="Shape To:" || str_shape2 =="Shape From"

|| radiovalue=="" || str_carat1==null || str_carat2==null)

{

Toast.makeText(getApplicationContext(), "Please enter all the field", Toast.LENGTH_SHORT).show();

}

else

{

new Thread(new Runnable() {

public void run() {

Search();

}

}).start();

}}

});

}

void Search(){

try{

httpclient=new DefaultHttpClient();

httppost= new HttpPost("http://192.168.43.6/test/search.php"); // make sure the url is correct.

//add your data

nameValuePairs = new ArrayList<NameValuePair>(2);

// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,

nameValuePairs.add(new BasicNameValuePair("carat1",unique_id.getText().toString().trim()));

nameValuePairs.add(new BasicNameValuePair("carat2",unique_id.getText().toString().trim()));

nameValuePairs.add(new BasicNameValuePair("color1",unique_id.getText().toString().trim()));

nameValuePairs.add(new BasicNameValuePair("color2",unique_id.getText().toString().trim()));

nameValuePairs.add(new BasicNameValuePair("shape1",unique_id.getText().toString().trim()));

nameValuePairs.add(new BasicNameValuePair("shape2",unique_id.getText().toString().trim()));

nameValuePairs.add(new BasicNameValuePair("stones",unique_id.getText().toString().trim()));

// $Edittext_value = $_POST['Edittext_value'];

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

//Execute HTTP Post Request

response=httpclient.execute(httppost);

ResponseHandler<String> responseHandler = new BasicResponseHandler();

final String response = httpclient.execute(httppost, responseHandler);

System.out.println("Response : " + response);

runOnUiThread(new Runnable() {

public void run() {

tv.setText("Response from PHP : " + response);

dialog.dismiss();

}

});

if(response.equalsIgnoreCase("Search")){

runOnUiThread(new Runnable() {

public void run() {

Toast.makeText(QuickSearch.this, "Searching Successful", Toast.LENGTH_SHORT).show();

}

});

// startActivity(new Intent(QuickSearch.this, LoginScreen.class));

//f1.putExtra("unique_id", unique_id.getText());

//st/artActivity(f1);

}else{

showAlert();

}

}catch(Exception e){

dialog.dismiss();

System.out.println("Exception : " + e.getMessage());

}

}

public void showAlert(){

QuickSearch.this.runOnUiThread(new Runnable() {

public void run() {

AlertDialog.Builder builder = new AlertDialog.Builder(QuickSearch.this);

builder.setTitle("Searching Error.");

builder.setMessage("Problem in Searching. Please try again later")

.setCancelable(false)

.setPositiveButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

}

});

AlertDialog alert = builder.create();

alert.show();

}

});

}

}

logcat error

05-15 11:13:22.327 28644-28932/com.diamond.traders I/System.out﹕ Exception : Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

网友答案:

You have commented out the line that initializes your unique_id EditText instance:

// unique_id=(EditText)findViewById(R.id.unique_id);

so when calling

nameValuePairs.add(new BasicNameValuePair("carat1",unique_id.getText().toString().trim()));

it throws an exception.

Initialize your gui elements properly to be able to access them.

分享给朋友:
您可能感兴趣的文章:
随机阅读: