Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looking at that code, the issue might be with how you are initializing name and search.</p> <p>Change your code:</p> <pre><code>name = (EditText) findViewById(getResources().getIdentifier("etName", "id", getPackageName())); search = (Button) findViewById(getResources().getIdentifier("btnSearch", "id", getPackageName())); </code></pre> <p>to this:</p> <pre><code>name = (EditText) findViewById(R.id.etName); search = (Button) findViewById(R.id.btnSearch); </code></pre> <p>Also, add a TextView to display your answer into your <code>main.xml</code> file, give it an id (let's say display for example) <code>android:id="@+id/display"</code> and then add it to your activity so you can change it:</p> <pre><code>display = (TextView) findViewById(R.id.display); </code></pre> <p>and then you can update it by calling:</p> <pre><code>display.setText('some text'); </code></pre> <p>Thus the start of your file should look like this:</p> <pre><code>public class WalmartActivity extends Activity { /** Called when the activity is first created. */ EditText name; Button search; TextView display; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (EditText) findViewById(R.id.etName); search = (Button) findViewById(R.id.btnSearch); display = (TextView) findViewById(R.id.display); .... </code></pre> <p>Next update your <code>if</code> statements from:</p> <pre><code>if (name.equals("Electronics")) </code></pre> <p>to:</p> <pre><code>if (name.getText().toString().equals("Electronics")) { display.setText("something"); } </code></pre> <p>An example piece of code:</p> <p>Demo2Activity.java</p> <pre><code>package com.demo1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Demo2Activity extends Activity { private Button button; private EditText editText; private TextView textView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); button = (Button) findViewById(R.id.button); editText = (EditText) findViewById(R.id.editText); textView = (TextView) findViewById(R.id.textText); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (editText.getText().toString().equals("Electronics")) { textView.setText("found"); } } }); } } </code></pre> <p>main2.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="main2.xml" android:id="@+id/textText" /&gt; &lt;EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText"/&gt; &lt;Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="button" /&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload